如何重写文件的第一行? [英] How rewrite first line of a file ?

查看:97
本文介绍了如何重写文件的第一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何重写一行的第一行而又不复制文件并重写它.我正在使用顺序写入,例如我的文件包含数据

How can i rewrite the first line of a line with out making a copy of file and rewriting it. I am using sequential writing for example my file contains data

hey<br />
how are you everyone at codeproject??



我想将其重写为



and i want to rewrite it as

hello!!<br />
how are you everyone at codeproject??



我尝试过此代码



i tried this code

ofstream fout2("res.dat",ios::app);
	fout2.seekp(0, ios::beg);
	fout2<<str;
	fout2.close();


但是它将str写入文件的末尾:(

有任何建议吗?


but it write str to the end of the file :(

Any suggestions ?

推荐答案

您不能这样做.
您必须将文件内容复制到内存缓冲区,然后按照您所描述的方式重写文件.
:-)
You cannot do that.
You have to copy file content to a memory buffer and then rewrite the file the way you depicted.
:-)


如果您的第一行始终是固定大小,则可以这样做.否则,您必须使用修改后的数据重写新文件.
If your first line is always of a fixed size, you can do that. Else, you have to rewrite a new file with the modified data.


讨论的一些想法

这篇关于如何重写文件的第一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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