Hello, the institute was asked to write a console application that works with a txt file (write / read). Implemented the task, but there was a problem with the encoding, the teacher advised to use wide characters, after using them the program stopped working, and when reading from the file it looped.

Here is the code of the file with the class:

class my { public: vector <int> number; vector <wstring> name; vector <wstring> patronymic; vector <wstring> surname; vector <wstring> sex; vector <wstring> stat; vector <wstring> guest; void output_workers(); void add_workers(); void del_workres(); void memory_clear(); }; И код функции: void my::add_workers() { int in; wstring st; char ch, point; float fl; wifstream f("db.txt"); f.imbue(locale(".ACP")); while (true) { if (!f.eof()) { f >> in; number.push_back(in); f >> st; name.push_back(st); f >> st; patronymic.push_back(st); f >> st; surname.push_back(st); f >> st; sex.push_back(st); f >> st; stat.push_back(st); f >> st; guest.push_back(st); } else break; } if ( number.size() < 98) { cout << "Внесение в БД нового участника" << endl; cout << "Введите номер: "; cin >> in; for (int i = 0; i < number.size(); i++) { if (in == number[i]) { system ("cls"); cout << "Внесение в БД нового участника" << endl; cout << "Введённый вами номер уже существует..."<< endl; cout << "Введите новый: "; cin >> in; i = -1; } } number.push_back(in); wcout << L"Введите имя: "; wcin >> st; name.push_back(st); wcout << L"Введите отчество: "; wcin >> st; patronymic.push_back(st); wcout << L"Введите фамилию: "; wcin >> st; surname.push_back(st); wcout << L"Выберете пол: "<<endl<< L"1 - Мужской;"<<endl<< L"2 - Женский"<<endl; wcin >> st; sex.push_back(L"Мужской"); wcout << L"Выберете статус участника: "<<endl<< L"1 - Студент"<<"2 - Аспирант"<< L"3 - Преподаватель"<<endl; wcin >> st; stat.push_back(L"Студент"); wcout << L"Выберете роль участника: "<<endl<<L"1 - Выступающий"<<endl<< L"2 - Гость"<<endl; wcin >> st; guest.push_back(L"Выступающий"); system ("cls"); wcout << "Вы уверены что хотите добавить участника: " << endl; wcout << name[name.size()-1] << " "; wcout << patronymic[patronymic.size()-1] << " "; wcout << surname[surname.size()-1] << " "; //cout << sex[sex.size()-1] << " "; //cout << stat[stat.size()-1] << " "; wcout << guest[guest.size()-1] << " (y/n?) "; cin >> point; if (point == 'y') { for(int i = guest.size()-1; i > 0; i--) for (int j = 0; j < i; j ++) { if (number[j] > number[j+1]) { swap(number[j],number[j+1]); swap(name[j],name[j+1]); swap(patronymic[j],patronymic[j+1]); swap(surname[j],surname[j+1]); swap(sex[j],sex[j+1]); swap(stat[j],stat[j+1]); swap(guest[j],guest[j+1]); } } wofstream of("db.txt"); for (int i = 0; i < guest.size(); i++) { of << number[i] << " "; of << name[i] << " "; of << patronymic[i] << " "; of << surname[i] << " "; of << sex[i] << " "; of << stat[i] << " "; if (i+1 < guest.size()) of << guest[i] << endl; else of << guest[i]; } } memory_clear(); system ("cls"); } else { cout << "Отказ от выполнения операции" << endl; system ("pause"); system ("cls"); } } 

Loops in this place:

 while (true) { if (!f.eof()) { f >> in; number.pushback(in); 

And why - I can not understand. Tell me who can.

  • Probably not getting hung up, but waiting for input (hanging on the operator f >> in)? If so, then apparently waiting for a decimal number , but at the entrance it is not. Rewrite the input on stdio and everything will become clear. - avp
  • Having seen enough of the number of questions that have appeared lately, in C ++ and Russian characters in the console, I want to give a foolish advice: do not write on the pluses , keep it simple. - avp

1 answer 1

Look at the answer to this question , perhaps not entirely predictable behavior is due to the mechanics of stream::eof() .

And, by the way, it is considered a good-tone rule not to use std::wcout and std::cout (as well as a couple of std::wcin and std::cin ) within the same application.