0
#include <sstream> #include <fstream> #include <codecvt> #include <deque> #include <iostream> #include <fcntl.h> #include <io.h> int main() { _setmode(_fileno(stdout), _O_U8TEXT); std::wstring wstr; std::locale::global(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>)); std::deque<std::wstring> lines, words; std::wifstream wif(L"C:/Users/Administrator/source/repos/ConsoleApplication1/x64/Debug/data.txt"); if (wif.is_open()) { std::wstringstream wss; wss << wif.rdbuf(); wstr = wss.str(); wif.close(); } else { std::wcout << L"Не удалось открыть файл!"; } { std::wstringstream wss(wstr); std::wstring temp; while (std::getline(wss, temp, L'\n')) lines.push_back(temp); } { for (std::wstring i : lines) { std::wstringstream wss(i); std::wstring temp; while (std::getline(wss, temp, L' ')) words.push_back(temp); } } std::wcout << words[6]; } 
01.01.2014 Старт Кошелёк 990 Изначально в кошельке было 990 руб 01.01.2014 Старт Карта 7000 А на карте 7000 20.09.2014 Кошелёк Проезд 70 Заплатил за проезд 20.09.2014 Кошелёк Базар 500 Купил фруктов на базаре asd 2014-09-21 Карта Книги 770.31 Купил книжку по карточке 2014-09-21 Карта Кошелёк 770.31 21.02.2014 Карта Кошелёк 1000 Снял тысячу в банкомате - - - - - 

UPD. I need to skip lines with invalid date, invalid format... I forgot my iss... in order to fix line by line appending to deque... we do not care about commentary existance we care about existance 4 first items like date, source, destination, amount..

2
  • 1
    You may want to post to ru.stackoverflow Commented Nov 25, 2021 at 19:22
  • @ThomasMatthews, I trust this community more. Commented Nov 25, 2021 at 19:23

1 Answer 1

1

Since the comment text has spaces in it, use std::getline() instead of operator>> to read it, eg:

if (!((iss >> date_ >> source_ >> destination_ >> amount_) && getline(iss, comment_))) 

Alternatively:

if (!(iss >> date_ >> source_ >> destination_ >> amount_)) { continue; } if (!getline(iss, comment_)) { continue; } 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, however I still stuck at being unable to read anything. What is really surprises me.
@Lith are you saying that while (std::getline(infile, line)) is not reading anything at all?
I found some other stackoverflow answers, thankfully I am still unable to parse line by line this file. I only got one element a first line and not fully. My output is : Файл прочитан, закрываем!01.01.2014 Старт
@Lith what's with the for loop that is removing L'\0' characters? After UTF-8 decoding, a text file shouldn't have any null characters in it. Besides, your while loop is calling getline() with L'\0' for the delimiter, so wline won't have any L'\0' in it anyway, and you not even using the stripped convert for anything, so the for loop is completely wasted. Also, your outer while loop should be calling getline() with L'\n' as the delimiter, not L'\0'.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.