When I tried to write the txt parser, there was a problem, everything works, but the console application almost immediately closes with an error. By searching, I found that strcpy is causing the bug. Why is this and how to fix it?

data.txt

Гришин Tmsu 2 Ардашев Tmsu 3 Котов Smdf 4 Евсеев Smdf 3 Новиков LLsd 2 

Code

 #include <iostream> #include <fstream> #include <cstring> #include <string> using namespace std; struct data_box { char second_name[50]; char subject_name[50]; int subject_val; }; int main() { setlocale(LC_ALL, "Russian"); struct data_box data_arr[4]; char data_second_name[50], data_subject_name[50]; int data_subject_val; int i,j = 0; std::ifstream data_file; std::string str; data_file.open("data.txt"); while (data_file >> data_second_name >> data_subject_name >> data_subject_val) { strcpy(data_arr[j].second_name, data_second_name); strcpy(data_arr[j].subject_name, data_subject_name); data_arr[j].subject_val = data_subject_val; j++; } for (i = 0; i < (sizeof(data_arr)/sizeof(data_arr[0])); i++){ cout << data_arr[i].second_name; } data_file.close(); return 0; } 

The output of the program:

 ГришинАрдашевКотовЕвсеев Process returned -1073741819 (0xC0000005) execution time : 9.177 s Press any key to continue. 
  • Why don't you use 'std :: string str'? Give specific error messages, etc. Create a minimal reproducible example . - αλεχολυτ
  • @alexolut updated but the problem is not in the performance, but in the syntax error with the address in strcpy - Happy_Cougar
  • 2
    You have 5 lines in the file, and an array of 4 elements. Increase the size of the array or use 'std ::: vector'. Cannot access elements with indices outside the array. If there was a problem in the syntax, the code would not compile at all. - αλεχολυτ
  • @alexolut You are right, I really didn’t read it. Do not tell me, but is there an opportunity to make this array dynamic? How many did not google, did not see a dynamic option. - Happy_Cougar
  • @Happy_Cougar you have already written - use vector - 0xdb

2 answers 2

You have 5 lines in the file, and an array of 4 elements. Increase the size of the array or use std::vector . Cannot access elements with indices outside the array.

If you decide to use std::vector , then call push_back to add a new element. Read more in the class documentation .

     #include <iostream> #include <fstream> #include <string> #include <vector> using namespace std; struct data_box { string second_name; string subject_name; int subject_val; }; int main() { setlocale(LC_ALL, "Russian"); vector<data_box> data_arr; ifstream data_file("data.txt"); if (!data_file.is_open()) exit(EXIT_FAILURE); data_box tmp; while(data_file >> tmp.second_name >> tmp.subject_name >> tmp.subject_val) data_arr.push_back(tmp); for (int i = 0; i < data_arr.size(); i++) cout << data_arr[i].second_name << endl; return 0; } 

    or with -std = c ++ 11

     int main() { setlocale(LC_ALL, "Russian"); vector<data_box> data_arr; ifstream data_file("data.txt"); if (!data_file) exit(EXIT_FAILURE); data_box tmp; while(data_file >> tmp.second_name >> tmp.subject_name >> tmp.subject_val) { data_arr.push_back(move(tmp)); } for (const auto &data: data_arr) cout << data.second_name << endl; return 0; }