简体   繁体   中英

Reading and searching binary files in c++

In school we are learning how to use binary files in c++ in Visual Studio. This piece of code works perfectly in Visual Studio 2005 but in version 2010 - 2013 it doesn't. It gives a reading violation error. So i hope one of you can help me with this because even my teacher doesn't know whats wrong :( The error happens at the end of the read. I've tried different method of ifstream and ofstream but with no succes.

My code:

#include <z:/Yoshi On My Mac/Google Drive/School/2013-2014/C-taal/headeryoshi.h>
#define B "z:/Yoshi On My Mac/Google Drive/city.dat"
typedef struct city {
        string zip, name;
};
void add() {
    ofstream file;
    city city;
    titelscherm("ADD CITY");
    cout << "ZIP: ";
    getline(cin, city.zip);
    while (city.zip not_eq "0") {
            cout << "Name: ";
            getline(cin, city.name);

            file.open(B, ios::app | ios::binary);
            file.write((char*)&city, sizeof(city));
            file.close();

            titelscherm("ADD CITY");
            cout << "POSTCODE: ";
            getline(cin, city.zip);
    }
    cout << "city: ";
    file.close();
}
void read() {
    ifstream file;
    city city;
    titelscherm("READ CITY");
    file.open(B, ios::in | ios::binary);
    file.read((char*)&city, sizeof(city));
    while (!file.eof()) {
            cout << city.zip << " ";
            cout << city.name << endl;
            file.read((char*)&city, sizeof(city));
    }
    file.close();
    _getch();      
}
void search() {
    string zip;
    city city;
    ifstream file;
    bool find;

    titelscherm("SEARCH ZIP");
    cout << "ZIP: ";
    getline(cin, zip);

    file.open(B, ios::in | ios::binary);
    if (!file.is_open()){
            cout << "FILE ERROR";
    }
    else {
            do {
                    file.read((char*)&city, sizeof(city));
                    find = (city.zip == zip);
            } while (!file.eof() and !find);

            if (find) {
                    cout << city.name << endl;
            }
            else {
                    cout<<" zit niet in het file" << endl;
            }
    }
    _getch();
    file.close();
}
int main() {
    add();
    read();
    search();
    return 0;
}

I would have serious doubts about your teacher's C++ abilities.

You cannot read std::string as raw data. It is not a POD type.

file.read((char*)&city, sizeof(city))
...
file.write((char*)&city, sizeof(city));

This code should NOT have worked previously, but it sounds like you somehow got real lucky.

You will need to serialize the strings out by writing their length, followed by the actual characters. When you read, you will read the size first, then allocate storage, then read the characters.

If you want to use your approach instead, then change the string values in your structure to char arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM