C ++: чтение файла CSV в массив массивов
Я работаю над заданием, где мне нужно прочитать CSV-файл с неизвестным количеством строк в структурированный массив. Только через C ++, а не C (они не хотят, чтобы мы объединяли оба).
Итак, у меня есть следующий код:
// DEFINITION
struct items {
int ID;
string name;
string desc;
string price;
string pcs;
};
void step1() {
string namefile, line;
int counter = 0;
cout << "Name of the file:" << endl;
cin >> namefile;
ifstream file;
file.open(namefile);
if( !file.is_open()) {
cout << "File "<< namefile <<" not found." << endl;
exit(-1);
}
while ( getline( file, line) ) { // To get the number of lines in the file
counter++;
}
items* item = new items[counter]; // Add number to structured array
for (int i = 0; i < counter; i++) {
file >> item[i].ID >> item[i].name >> item[i].desc >> item[i].price >> item[i].pcs;
}
cout << item[1].name << endl;
file.close();
}
Но когда я запускаю код, приложение вернет после чтения пробел, и я на самом деле думаю, что оно вообще не читается. Вот вывод в консоли:
Name of the file:
open.csv
Program ended with exit code: 0