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

半世苍凉 提交于 2019-12-24 01:39:08

问题


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.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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/2317029/c-read-and-write-to-a-binary-file-at-the-same-time

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