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.