写入现有二进制文件 c++ 的中间

发布于 2024-11-16 09:13:25 字数 393 浏览 7 评论 0原文

我正在尝试打开一个二进制文件进行写入而不擦除内容。但我不想写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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

噩梦成真你也成魔 2024-11-23 09:13:25

尝试 seekp 的第二个重载,它允许您提供偏移量和方向,这可能是您的情况下的文件开头(即 ios_base::beg )。当然,这假设您知道自己在做什么,并且您想要做的就是覆盖现有的字符数。

编辑:这是完整的工作示例:

#include <iostream>
#include <fstream>

using namespace std;
int main()
{
  {
    ofstream out("test.txt", ios::binary);
    for(int i = 0; i < 100; i++)
      out.put('_');
    out.write("Hallo", 5);
  }

  {   
    fstream out2("test.txt", ios::binary | ios::out | ios::in);
    out2.seekp(10, ios::beg);
    out2.write("Welt", 4);
  }
}

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:

#include <iostream>
#include <fstream>

using namespace std;
int main()
{
  {
    ofstream out("test.txt", ios::binary);
    for(int i = 0; i < 100; i++)
      out.put('_');
    out.write("Hallo", 5);
  }

  {   
    fstream out2("test.txt", ios::binary | ios::out | ios::in);
    out2.seekp(10, ios::beg);
    out2.write("Welt", 4);
  }
}
酒绊 2024-11-23 09:13:25

当使用 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.

咿呀咿呀哟 2024-11-23 09:13:25

您无法神奇地从中间扩展文件。也许最容易写入新文件:首先复制初始段,然后写入新数据,然后复制剩余段。全部完成后,您可以覆盖原始文件。

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.

掐死时间 2024-11-23 09:13:25

根据这里的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.

德意的啸 2024-11-23 09:13:25

工作代码:此代码在 cout.exe 中搜索字符串 (OLD-STRING) 并替换为新字符串 (NEW-STRING)。

`#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
  fstream ifs;
  ifs.open ("C:\\Users\\user\\Desktop\\cout.exe", fstream::binary | fstream::in | fstream::out);

  std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());

  size_t pos = str.find("OLD-STRING");

  if (pos != string::npos)
  {
    cout << "string found at position: " << int(pos) << endl;

    ifs.seekp(pos);

    ifs.write("NEW-STRING", 10);

  }
  else
  {
    cout << "could not find string" << endl;
  }

  if (ifs.is_open())
    ifs.close();

  return 0;
}`

Working Code: This code searches for a string (OLD-STRING) in cout.exe and replaces with a new string (NEW-STRING).

`#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
  fstream ifs;
  ifs.open ("C:\\Users\\user\\Desktop\\cout.exe", fstream::binary | fstream::in | fstream::out);

  std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());

  size_t pos = str.find("OLD-STRING");

  if (pos != string::npos)
  {
    cout << "string found at position: " << int(pos) << endl;

    ifs.seekp(pos);

    ifs.write("NEW-STRING", 10);

  }
  else
  {
    cout << "could not find string" << endl;
  }

  if (ifs.is_open())
    ifs.close();

  return 0;
}`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文