[英]How do I overwrite a single byte in a binary file using fseek() and fwrite()?
I am trying to overwrite a single byte in a binary file.我正在尝试覆盖二进制文件中的单个字节。 I am using
fseek()
to set the stream position indicator to the byte that I want to overwrite.我正在使用
fseek()
将流位置指示器设置为我想要覆盖的字节。 Then I am calling to fwrite()
with a new byte in that location.然后我在那个位置用一个新字节调用
fwrite()
。 Somehow the other bytes in the file are being affected.文件中的其他字节不知何故受到影响。 Heres some code:
继承人一些代码:
bool x = 1;
bool y = 0;
bool z = 1;
/* WRITE 101 to file */
FILE *ff = fopen(file, "wb");
fwrite(&x, sizeof(x), 1, ff);
fwrite(&y, sizeof(y), 1, ff);
fwrite(&z, sizeof(y), 1, ff);
fclose(ff);
bool x_r, y_r, z_r;
/* READ from file */
ff = fopen(file, "rb");
fread(&x_r, sizeof(x_r), 1, ff);
fread(&y_r, sizeof(y_r), 1, ff);
fread(&z_r, sizeof(z_r), 1, ff);
fclose(ff);
/* PRINT to standard output */
cout << x_r << y_r << z_r << endl; // OUTPUT: 101
bool overwrite = 1;
/* OVERWRITE */
ff = fopen(file, "wb");
fseek(ff, 1, SEEK_SET);
fwrite(&overwrite, sizeof(overwrite),1 , ff);
fclose(ff);
/* READ from file */
ff = fopen(file, "rb");
fread(&x_r, sizeof(x_r), 1, ff);
fread(&y_r, sizeof(y_r), 1, ff);
fread(&z_r, sizeof(z_r), 1, ff);
fclose(ff);
/* PRINT to standard output */
cout << x_r << y_r << z_r << endl; // OUPUT 011; I was expecting 111
The above code basically writes three bytes of data to a file.上面的代码基本上是将三个字节的数据写入一个文件。 101. Then I use fseek() to set to reposition the stream indicator to point to the second byte.
101. 然后我使用 fseek() 设置重新定位流指示器以指向第二个字节。 But calling fwrite() to overwrite the second byte from 0 to 1 is changing the first byte as well.
但是调用 fwrite() 来覆盖第二个字节从 0 到 1 也会改变第一个字节。 The expected output is 111, but I am getting 011.
预期输出为 111,但我得到 011。
Any ideas why this is happening?任何想法为什么会发生这种情况? I tried putc as well, but that didn't work out either.
我也试过 putc ,但也没有用。 Any help would be appreciated.
任何帮助,将不胜感激。
“ r +”模式打开文件以供读取和写入,但保留其内容。
1) use fopen(filename, "r+"); 1)使用fopen(filename,“ r +”);
2) seek to the position of the byte or use fread until you find the byte 2)寻找字节的位置或使用fread直到找到字节
3) save the offset of the byte 3)保存字节的偏移量
3) fseek(fileHandle, offset, SEEK_SET) 3)fseek(fileHandle,offset,SEEK_SET)
4) fwrite(addr_byte, 1U, 1U, fileHandle) 4)fwrite(addr_byte,1U,1U,fileHandle)
int fun_write(void *data, int reg_offset, int byte_count) {
FILE *fp;
fp = fopen( "file" , "r+" );
fseek(fp, reg_offset, SEEK_SET);
fwrite(data , 1 , byte_count , fp );
fclose(fp);
}
You have to open the file for append , rather than for write ; 您必须打开文件进行追加 ,而不是写入 ; ie,
即
FILE *ff = fopen(file, "rb+");
Write mode truncates the file to zero length as soon as you open it. 打开文件后,写入模式会将文件截断为零长度。
Adams comment is correct. 亚当斯的评论是正确的。
"wb" completely replaces the file. “ wb”完全替换文件。 Since you start writing at position 1, the function has to put something in position 0, so it puts a NUL byte there.
由于您从位置1开始写入,因此该函数必须将某些内容放置在位置0中,因此它将NUL字节放置在此处。
I agree that you need to use "r+b" 我同意您需要使用“ r + b”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.