读取文件并存储在缓冲区中. [英] Reading a file and storing in a buffer.

查看:98
本文介绍了读取文件并存储在缓冲区中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用fstream读取文件,
并将内容存储在char缓冲区中.
它的大小是1200个字节.
我想通过删除内容并将某些字节附加到临时文件中,将该缓冲区写入临时文件中.
下面的例子

Hi Guys,

I am reading a file using fstream,
and storing the contents in a char buffer.
its size is 1200 bytes.
i want to write that buffer into a temp file by deleting contents and appending some bytes to the temp file.
example below

for(i=0;i<buffesize;i++)>
{
if i>200 && i<205) continue; 
// here it skips the 4bytes.i want to append in same position which existing should
// not print
tempfile<<buffer[i];
}



在写入文件之前,我想在第200个位置删除4个字节,并在同一位置附加8个字节.

谁能在这方面提供帮助


感谢



before writing into a file i want to delete 4bytes in a 200th position and append 8 bytes in the same poistion.

can anyone help in this regard


thanks

推荐答案

您可以执行此操作,而无需亲自操作缓冲区.
只需使用seekg即可将读取指针设置到正确的位置.
然后寻求将写指针设置到正确的位置.
当然,您还需要考虑文件大小,不能超出文件末尾.

另请参见此链接:
http://www.cplusplus.com/reference/iostream/fstream/ [
You can do this without manipulating the buffer yourself.
Just use seekg to set the readpointer to the right position.
And seekw to set the writepointer to the right position.
Of course you also need to take the filesize into account, you cannot seek beyond the end of the file.

see also this link:
http://www.cplusplus.com/reference/iostream/fstream/[^]


一旦有了数据在缓冲区中,您可以使用write()函数一次写出许多字符,如
Once you have your data in the buffer you can write out a number of characters at a time with the write() function, as described here[^]. You just need to write the first block from the buffer, then the 8 new characters, and lastly the second block from the buffer.


这篇关于读取文件并存储在缓冲区中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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