I am writing a program, it is necessary to read information from a txt file and write information to the structure

struct disp_res{ int x; int y; }; struct NOTEBOOK{ disp_res size_display; int regeneration_frequency; float diagonal; float size_hdd; float price; char model; }; 

wrote the following code for this:

 int main() { SetConsoleOutputCP(CP_UTF8); string name_file="C:\\Users\\Home\\Desktop\\AlgoritmAndStructData\\Lab1\\Lab1\\note.txt"; int strInFile; string buffer; nach: ifstream file(name_file); if (file.is_open()){ strInFile = getQuantityString(file); NOTEBOOK* array = new NOTEBOOK[strInFile]; int i = 0; file.close(); ifstream file1(name_file); while(!file1.eof()){ getline(file1, buffer); array[i].size_display.x=stoi(split(buffer)); array[i].size_display.y=stoi(split(buffer)); array[i].regeneration_frequency=stoi(split(buffer)); array[i].diagonal=stof(split(buffer)); array[i].size_hdd=stof(split(buffer)); array[i].price=stof(split(buffer)); array[i].model=strToChar(split(buffer)); i++; } outputFile(array, strInFile); } else{ if(dialog()) goto nach; } return 0; } 

the split method divides a line by a tab

 string split(string str){ string delimiter ="\t", pos_string; int position_delimiter = 0; position_delimiter = str.find(delimiter); if (position_delimiter==-1) return str; else{ pos_string = str.substr(0, position_delimiter); str.erase(0, position_delimiter + delimiter.length()); return pos_string; } } 

getQuantityString method counts the number of lines in a file

 int getQuantityString(ifstream &files){ int quantity = 0; string buffer =""; while (!files.eof()){ getline(files, buffer); quantity++; } return quantity; } 

gives an error when trying to convert a string to an int string in the main method

 array[i].size_display.x=stoi(split(buffer)); 

enter image description here

The contents of the note.txt file enter image description here UTF-8 file encoding. What am I doing wrong, why does the stoi() method not work? Debugging looked reading is normal ... well, or so it seems to me enter image description here Split method works correctly enter image description here

  • This is most likely not the cause of your troubles, but still an error: while(!file1.eof()){ - this is not done ... See ru.stackoverflow.com/questions/833980/… - Harry
  • goto nach; reinitializes file resulting in undefined behavior. The situation when the string in the split empty is not processed. - VTT
  • goto nach is not used now in the program in principle. when the string is empty, this is equivalent to the fact that there is no delimiter, i.e. will return an empty string. but this does not reach in principle. because Everything breaks at the first iteration. - i_burykin pm
  • about while (! file1.eof ()) I have a special function that counts the number of lines in the file, and I use there while (! file.eof) and there are no problems with the counting of lines. Previously, it may have been such an error, but this piece of code works correctly for me - i_burykin

0