开发者

C++, read and write to a binary file at the same time

开发者 https://www.devze.com 2022-12-21 03:32 出处:网络
I wanted to know if it\'s possible to read a byte from a binary file with \"fstream\" and then change that byte and write it back.

I wanted to know if it's possible to read a byte from a binary file with "fstream" and then change that byte and write it back. I tried this code but it didn't work and nothing happens but I'm sure it reads correctly.

file.open(path, ios::in|ios::out|ios::binary|ios::ate);
file.开发者_StackOverflow社区seekg(0, ios::end);
int size=file.tellg();
file.seekg(0,ios::beg);
char buffer;    
for(int i=0;i<size;i++)
{
    file.read((char*)&buffer,sizeof(char));
    buffer=(buffer+7)%256;
    file.write((char*)&buffer, sizeof(char));
}

should I take the file pointer one byte back after reading like this:

file.seekg(-1, ios::cur);

Thanks in advance.


Yes. As you suggested in the question, you need to adjust the position of the file pointer using seekg if you want to overwrite the previously read bytes. After each read, the file pointer would be positioned after the read bytes.


You should move back the pointer the amount of previous read bytes; so with char -1.

file.seekg(-1, std::ios::cur);

Do consider that when modifying data it has to have the same byte size, or you'll overwrite other data in your file.


Reading and writing uses the same position pointer. Hence, if you want to be able to read consecutive values, to rewrite a value you are expected to rewind the pointer to the position that was read, right?

That said, in your case it would probably be more efficient to read blocks of data, then convert them in memory, and then write them back.


Firstly you must ALWAYS check the file actually opened:

file.open(path, ios::in|ios::out|ios::binary|ios::ate);
if ( ! file.is_open() ) {
    throw "open failed";
}

Then you must ALWAYS check that reads & writes that you expect to work have worked:

if ( ! file.read((char*)&buffer,sizeof(char)) ) {
   throw "read failed";
}

And lastly, you should check that the combination of open flags you are using is valid on your particular platform - see Why can't I read and append with std::fstream on Mac OS X? for some discussion of this - I would suggest removing the ios::ate flag in your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号