0

I am trying to read a struct from a binary file, but I have some problems. Here's the struct

struct note {
    int id;
    char str[20];
};

This is the code that writes to a binary file.

std::ofstream file("somefile.bin", std::ios::out | std::ios::binary);
if (file.is_open()) {
    // some code to read input into the struct
    file.write((char *) &mystruct, sizeof(mystruct));
}
file.close();

This works fine. But when I create another program to read from the binary file, I get some issues. The program compiles and it runs, but it doesn't read the file. Here's the code:

std::ifstream file("somefile.bin", std::ios::in | std::ios::binary) 
// somefile is the file I created in the first program.
if (file.is_open()) {
    while (!file.eof()) {
        file.read((char *) &otherstruct, sizeof(otherstruct));
        if (file.fail()) {
            std::cout << "File read error!";
            exit(1);
        }
        std::cout << "ID: " << otherstruct.id << "\nNote: " << otherstruct.str;
    }
} else
    std::cout << "File open error.";
file.close();

When I run the code I only get file read error. it means that this code executes.

if (file.fail()) {
    std::cout << "File read error!";
    exit(1);
}

Why isn't my code reading from the binary file?. Why is it failing to read the file?

  • [Maybe relevant](https://stackoverflow.com/a/3181093/509868) – anatolyg Jul 06 '22 at 16:52
  • If you wrote a `mystruct` to the file, you also need to read a `mystruct` from it, not some other type `otherstruct`. Also, when the read results in a failed state, that just means that you reached the end of file. The `file.eof()` check will not tell you whether or not there is still a `mystruct` to read in the file. – user17732522 Jul 06 '22 at 16:52
  • 2
    *The program compiles...* -- The relevancy of a program "compiling" means nothing. All it means is that there are no syntax errors. Whether the program has logic bugs is a totally different story. Too many new programmers think that "compiling without errors..." means that the program must run correctly, and if it doesn't, call the help desk (in this case, StackOverflow). What you should be doing is debugging your code to, at the very least, determine where the failure occurs. – PaulMcKenzie Jul 06 '22 at 16:54
  • "it means that this code executes"... your error trip. The way that loop is written that will eventually happen no matter what. – WhozCraig Jul 06 '22 at 16:54
  • Maybe check what is the error code from ifstream? – Taekahn Jul 06 '22 at 16:55
  • The other thing is that you need to be careful about the "binary file read/write" stuff. If that `struct` changes in any way so that it no longer is trivially-copyable, none of the code you wrote to read/write will work. – PaulMcKenzie Jul 06 '22 at 17:01
  • 1
    You need to be extremely explicit with respect to the error you are struggling with. In this case the duplicate is correct whether or not it's the unknown problem you're having simply because the duplicate describes a problem with your code. – user4581301 Jul 06 '22 at 17:05

0 Answers0