Этот кусок кода выводит последнюю структуру в файле два раза:
int printRec(cities city) {
    cout << "Name of the city: ";
    cout << city.name << "\n";
    cout << "Population of the city: ";
    cout << city.pop << "\n";
    cout << "Area of the city: ";
    cout << city.area << "\n";
    cout << "Year of foundation of the city: ";
    cout << city.year << "\n";
    cout << "Number of schools in the city: ";
    cout << city.schools << "\n";
    return 0;
}
int readRec(char path[]) {
    cities city;
    ifstream in(path);
    while (!in.eof()) {
        in.read((char *) &city, sizeof(cities));
        printRec(city);
    }
    in.close();
    return 0;
}
Хотя если записать две структуры и посмотреть на файл, там их действительно две. А выводит три.
До этого записывается в файл вот таким образом:
int readStr(cities &city){
    cout << "Input information.\n";
    cout << "Name of the city: ";
    cin >> city.name;
    cout << "Population of the city: ";
    cin >> city.pop;
    cout << "Area of the city: ";
    cin >> city.area;
    cout << "Year of foundation of the city: ";
    cin >> city.year;
    cout << "Number of schools in the city: ";
    cin >> city.schools;
    return 0;
}
int addStr(char path[]) {
    cities city;
    do {
        readStr(city);
        ofstream out((path), ios::app);
        out.write((char *) &city, sizeof(cities));
        out.close();
        cout << "If you wish to enter another entry, press 1.\n";
        cout << "Otherwise, press 0: ";
        cin >> city.area;
    }
    while (city.area);
    return 0;
}
Собственно вопрос - почему?
path - путь к файлу.
Сама структура:
struct cities {
public:
    char name[25];
    int pop;
    int area;
    int year;
    int schools;
};
Если тут всё нормально, и не должно выводить структуру два раза, так и напишите. Буду искать ошибки в вызове.
Отправлено: 22 Ноября 2011, 14:52
Разобрался. Тему можно закрывать/удалять