文件查找/覆盖字节 [英] File Seeking / Overwriting bytes

查看:57
本文介绍了文件查找/覆盖字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要打开一个现有文件,寻找一个位数为X的

字节,并写出Y个字节数覆盖任何现有字节,

但不删除任何其他数据。这可能吗?


我已经打开了一个追加a的文件模式,然后使用fseek和
SEEK_SET来寻找文件中的0个字节,但这会在现有数据的末尾设置位置

,而不是实际在开始时文件。


所以说我有一个8字节的文件:0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF

0xFF

我想打开它,寻找一个特定的偏移量,让我们说2个字节,

然后写出4个字节,值1D覆盖现有字节,

离开文件with:0xFF 0xFF 0x1D 0x1D 0x1D 0x1D 0xFF 0xFF


继承我的测试代码。


FILE * cont;

cont = fopen(" /home/jk/test.txt" ;," a");

fseek(续,0,SEEK_SET);

fputs( T,续);

fclose(续);

请有人指出我正确的方向,我想避免

阅读所有内容,编辑并重写,因为

文件可能很大。


非常感谢,

Jason

Hi,

I need to open an existing file, seek to a position at X number of
bytes, and write out Y number of bytes overwriting any existing bytes,
but no erasing any other data. Is this possible?

I''ve opened a file in append "a" mode, and then used fseek with
SEEK_SET to seek to 0 bytes into the file, but this sets the position
at the end of existing data, not actually at the start of the file.

So say I have a file of 8 bytes: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
0xFF
I want to open it, seek to a specific offset, lets say 2 bytes in,
then write out 4 bytes with the value 1D overwriting existing bytes,
leaving the file with: 0xFF 0xFF 0x1D 0x1D 0x1D 0x1D 0xFF 0xFF

Heres my testing code.

FILE *cont;
cont = fopen("/home/jk/test.txt", "a");
fseek(cont, 0, SEEK_SET);
fputs("T", cont);
fclose(cont);
Please can someone point me in the right direction, I want to avoid
reading everything in, editing it and rewriting it all out because the
file could be huge.

Many thanks,
Jason

推荐答案

Jason< je ****** @ gmail.comwrites:
Jason <je******@gmail.comwrites:




我需要打开一个现有文件,寻找一个位数为X的

字节的位置,并写出Y的数字字节覆盖任何现有字节,

但不删除任何其他数据。这可能吗?


我已经打开了一个追加a的文件模式,然后使用fseek和
SEEK_SET来寻找文件中的0个字节,但这会在现有数据的末尾设置位置

,而不是实际在开始时的文件。
Hi,

I need to open an existing file, seek to a position at X number of
bytes, and write out Y number of bytes overwriting any existing bytes,
but no erasing any other data. Is this possible?

I''ve opened a file in append "a" mode, and then used fseek with
SEEK_SET to seek to 0 bytes into the file, but this sets the position
at the end of existing data, not actually at the start of the file.



你想要r +模式。

You want the "r+" mode.


11月13日21:39,Nate Eldredge< n ... @ vulcan.lanwrote:
On 13 Nov, 21:39, Nate Eldredge <n...@vulcan.lanwrote:

Jason< jeche ... @ gmail.comwrites:
Jason <jeche...@gmail.comwrites:


Hi,


我需要打开一个现有文件,寻找一个位数为X的

字节的位置,并写出Y个字节覆盖任何现有的字节,

但没有删除任何其他数据。这可能吗?
I need to open an existing file, seek to a position at X number of
bytes, and write out Y number of bytes overwriting any existing bytes,
but no erasing any other data. Is this possible?


我已经在追加a中打开了一个文件。模式,然后使用fseek和
SEEK_SET来寻找文件中的0个字节,但这会在现有数据的末尾设置位置

,而不是实际在开始时的文件。
I''ve opened a file in append "a" mode, and then used fseek with
SEEK_SET to seek to 0 bytes into the file, but this sets the position
at the end of existing data, not actually at the start of the file.



你想要r +模式。


You want the "r+" mode.



啊,这很简单,非常感谢非常有效。

Ah that simple, thanks very much works perfectly.


Jason写道:
Jason wrote:




我需要打开一个现有文件,寻找一个位数为X的

字节,并写出Y个字节覆盖任何现有字节,

但不删除任何其他数据。这可能吗?
Hi,

I need to open an existing file, seek to a position at X number of
bytes, and write out Y number of bytes overwriting any existing bytes,
but no erasing any other data. Is this possible?



是的,对于合适的文件类型(也就是说,对于通过二进制而不是二进制访问时感觉为
的文件text" streams)。

Yes, for suitable file types (that is, for files that make
sense when accessed via "binary" rather than "text" streams).


我已经在附加的a中打开了一个文件。模式,然后使用fseek和
SEEK_SET来寻找文件中的0个字节,但这会在现有数据的末尾设置位置

,而不是实际在开始时的文件。
I''ve opened a file in append "a" mode, and then used fseek with
SEEK_SET to seek to 0 bytes into the file, but this sets the position
at the end of existing data, not actually at the start of the file.



"追加"表示所有新数据都在最后。如果你想在其他地方写入数据,请使用rb +和rb +。或者r + b,两者都是

表示打开现有文件进行阅读(r)和更新(+),

二进制模式(b )。

"Append" means "all new data goes at the end." If you want to
write data at some other spot, use "rb+" or "r+b", both of which
mean "open an existing file for reading (r) and update (+), in
binary mode (b)."


所以说我有一个8字节的文件:0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF

0xFF

我想打开它,寻找一个特定的偏移,让我们说2个字节,

然后写出4个字节,值1D覆盖现有字节,

保留文件:0xFF 0xFF 0x1D 0x1D 0x1D 0x1D 0xFF 0xFF


继承我的测试代码。


FILE * cont;

cont = fopen(" /home/jk/test.txt"," a");
So say I have a file of 8 bytes: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
0xFF
I want to open it, seek to a specific offset, lets say 2 bytes in,
then write out 4 bytes with the value 1D overwriting existing bytes,
leaving the file with: 0xFF 0xFF 0x1D 0x1D 0x1D 0x1D 0xFF 0xFF

Heres my testing code.

FILE *cont;
cont = fopen("/home/jk/test.txt", "a");



如上所述,使用rb +。另外,在实际程序中你应该检查I / O操作是否成功或失败,特别是fopen()

调用,因为它们特别容易失败。 />

Use "rb+", as noted above. Also, in a real program you should
check whether I/O operations succeed or fail, especially the fopen()
calls because they are particularly susceptible to failure.


fseek(cont,0,SEEK_SET);
fseek(cont, 0, SEEK_SET);



" 2 bytes in,"我想你说的?那为什么零?这个搜索

在文件的最开头定位流,而不是在第三个字节的


"2 bytes in," I think you said? Then why the zero? This seek
positions the stream at the very beginning of the file, not at at
the third byte.


fputs(" T",cont);
fputs("T", cont);



这写入单个字节''T',而不是所需的四个字节

''\ x1d''。

This writes the single byte ''T'', not the desired four bytes
of ''\x1d''.


fclose(续);


请有人指出我正确的方向,我想避免

读取所有内容,编辑它并重写它们因为

文件可能很大。
fclose(cont);
Please can someone point me in the right direction, I want to avoid
reading everything in, editing it and rewriting it all out because the
file could be huge.



-
Er **** *****@sun.com


这篇关于文件查找/覆盖字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆