写入现有二进制文件 c++ 的中间
我正在尝试打开一个二进制文件进行写入而不擦除内容。但我不想写eof。我想写入文件中的特定位置。
这是一个小例子:
ofstream out("test.txt", ios::binary | ios::app);
for(int i = 0; i < 100; i++)
out.put('_');
out.write("Hallo", 5);
out.close();
ofstream out2("test.txt", ios::binary | ios::app);
out2.seekp(10);
out2.write("Welt", 4);
out2.close();
如果使用应用程序,则搜索不起作用。如果不使用应用程序打开文件,则会删除数据。有人知道答案吗?
I'm trying to open a binary file for writing without erasing the content. But I do not want to write to eof. I want to write to a specific position in file.
Here is a litte example:
ofstream out("test.txt", ios::binary | ios::app);
for(int i = 0; i < 100; i++)
out.put('_');
out.write("Hallo", 5);
out.close();
ofstream out2("test.txt", ios::binary | ios::app);
out2.seekp(10);
out2.write("Welt", 4);
out2.close();
If using app, seek doesn't work. If not using app opening file erases data. Does anybody know an answer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试
seekp
的第二个重载,它允许您提供偏移量和方向,这可能是您的情况下的文件开头(即ios_base::beg )。当然,这假设您知道自己在做什么,并且您想要做的就是覆盖现有的字符数。
编辑:这是完整的工作示例:
try the second overload of
seekp
, which allows you to provide an offset and a direction, this could be begining of file in your case (i.e.ios_base::beg
). This of course assumes you know what you are doing and all you want to do is overwrite an existing number of characters.EDIT: here is fully working example:
当使用 ios::app 打开时,就好像您打开了一个恰好附加到现有文件的新文件:您无法访问现有文件。我不确定,因为我会按照 Kerrek 的回答做,但如果你真的想尝试,你可能必须用“ios::in | ios::out”打开,类似于fopen("test.txt", "rw").
或者正如 crashmstr 指出的那样: ios::out 可能就足够了。
When opening with ios::app, it is as if you open a new file that just happened to be attached to an existing file: you can not access the existing file. I'm not sure, because I would do as in Kerrek's answer, but if you really want to try, you probably have to open with "ios::in | ios::out", similar to fopen("test.txt", "rw").
Or as crashmstr points out: ios::out might be enough.
您无法神奇地从中间扩展文件。也许最容易写入新文件:首先复制初始段,然后写入新数据,然后复制剩余段。全部完成后,您可以覆盖原始文件。
You cannot magically extend the file from the middle. Perhaps easiest to write to a new file: First copy the initial segment, then write your new data, then copy the remaining segment. When all is done, you can overwrite the original file.
根据这里的fstream规范
fstream::open
ios::app “在每次输出操作之前将流的位置指示器设置为流的末尾。”所以 ios::app 不适用于替换,任何类型的搜索都会失败,至少对我来说。
仅使用 ios::out 确实会清除文件内容,仅保留大小,基本上将文件变成垃圾。
ios::in|ios::out 对我来说是唯一有用的东西。
According to the specification of fstream here
fstream::open
the ios::app "Sets the stream's position indicator to the end of the stream before EACH output operation." So ios::app doesn't work for replacing, seeks of any sort fail, at least for me.
Just using ios::out does wipe out the file contents preserving only the size, basically turning the file into trash.
ios::in|ios::out turned out as the only working thing for me.
工作代码:此代码在 cout.exe 中搜索字符串 (OLD-STRING) 并替换为新字符串 (NEW-STRING)。
Working Code: This code searches for a string (OLD-STRING) in cout.exe and replaces with a new string (NEW-STRING).