Задание: Считать данные из файла, занести их в структуру и вывести в консоль.
Структура данных в файле: ABLEC&C,Missha,350.00,100,10.02.2017,South Korea;
Не удается считать данные и занести их в динамический массив структур. Подскажите, пожалуйста, как можно исправить?
Код:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct Goods {
char company[40];
char name[40];
char country[50];
double price;
int amount;
struct {
int day;
int month;
int year;
} date;
};
int main() {
fstream file("goods.txt", ios::in);
if (file.is_open()) {
// находим количество записей о товарах в файле
int records = 0;
char stop;
for (file >> stop; !file.eof(); file >> stop)
if (stop == ';') records++;
// считываем структуру в файле и выводим в консоль
Goods* goods = new Goods[records];
char* token;
char line[256];
for (int i = 0; i < records; i++) {
cout << "Product " << i + 1 << endl;
file.getline(line, 256, ';');
token = strtok(line, ",");
strcpy(goods[i].company, token);
cout << "Company: " << goods[i].company << endl;
}
}
else cerr << "Error with opening file." << endl;
file.close();
}
seek