Cannot read simple binary integers from file? (C++)

♀尐吖头ヾ 提交于 2019-12-04 03:48:18

try w.flush() or w.close() before r.read. the problem is when you write it usually bufferes text and doesn't save it in file. so there is nothing realy in file for r.read.

This code:

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    {
        ofstream w( "foo.txt" );
        int ints[10] = {0,1,2,3,4,5,6,8,9};
        w.write((char*)&ints, sizeof(ints));
    }
    {
        ifstream r( "foo.txt" );
        int in_ints[10];
        r.read((char*)&in_ints, sizeof(in_ints));
        for( int i = 0; i < 10; i++) {
            cout << in_ints[i] << " ";
        }
    }
}

prints:

0 1 2 3 4 5 6 8 9 0

Note there are some numbers missing from your initialisation.

Data written to a file isn't necessarily visible to other streams (in the same process or not) until the file is closed, or at least flushed. And if the file "foo.bin" doesn't exist before you start the program, opening it for reading will fail (and so all further use will be a no-op). Your program violates one of the most basic rules of programming: if anything can fail, always check that it hasn't before continuing. You don't check any of the results of your IO.

I might also add that reading and writing data this way is very, very fragile, and can generally only be guaranteed to work within the same process—even recompiling with a different version of the compiler or different compiler options may cause the data representation to change, with the results that you won't be able to read data written earlier. In the case of arrays of int, of course, this is highly unlikely as long as you don't change machine architectures. But in general, if you'll need to reread the data in the future, it's a technique best avoided. (The (char*) are reinterpret_cast, and as we know, reinterpret_cast is a very strong signal that there is a portability issue.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!