Reading in 4 bytes at a time

前端 未结 5 1479
暗喜
暗喜 2021-01-05 01:54

I have a big file full of integers that I\'m loading in. I\'ve just started using C++, and I\'m trying out the filestream stuff. From everything I\'ve read, it appears I c

5条回答
  •  青春惊慌失措
    2021-01-05 02:10

    To read a single integer, pass in the address of the integer to the read function and ensure you only read sizeof int bytes.

    int myint;
    
    //...
    
    fstr.read(reinterpret_cast(&myint), sizeof(int));
    

    You may also need to open the file in binary mode

    fstr.open("table.dat", std::ios::binary);
    

提交回复
热议问题