14

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?

Jonny Schubert
  • 1,393
  • 2
  • 18
  • 39

5 Answers5

12

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);
  }
}
Nim
  • 33,299
  • 2
  • 62
  • 101
  • I just tried out2.seekp(10, ios_base::beg);. That doesn't change anything. – Jonny Schubert Jun 21 '11 at 15:26
  • It should not erase the file unless you specify ios::trunc – crashmstr Jun 21 '11 at 15:28
  • @Jonny Schubert, can you post your full example? – Nim Jun 21 '11 at 15:31
  • using namespace std; int main() { ifstream in("test.txt", ios::binary | ios::app); ofstream out("test.txt", ios::binary); 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(); int a; cin >> a; return 0; } – Jonny Schubert Jun 21 '11 at 15:32
  • 1
    actually, @stefaanv has a point in his answer, try `fstream` rather than `ofstream`. – Nim Jun 21 '11 at 15:33
6

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.

stefaanv
  • 14,072
  • 2
  • 31
  • 53
  • 6
    you need both `ios::out` and `ios::in`, otherwise, it simply overwrites methinks.. – Nim Jun 21 '11 at 15:38
  • Yes because `ios::out` alone results in an open call of the following kind on linux : O_WRONLY|O_CREAT|O_TRUNC, while combining `ios::out | ios::in` results in an O_RDWR call, without the O_TRUNC. – daminetreg Oct 24 '12 at 12:22
5

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.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • I do not want to extend the file in the middle. I want to overwrite the inital values! – Jonny Schubert Jun 21 '11 at 15:25
  • @Jonny: That's not what you said in your question. You said "without erasing the content". Otherwise it's trivial, just open for write (without "append"), seek to the beginning and write. – Kerrek SB Jun 21 '11 at 15:36
  • I don't want to write to the beginning. I want to write somewhere in the file. The problem is opening the file again will erase the content. For example: File at the beginning: ______________________ File after manipulation: ______Bla_______ – Jonny Schubert Jun 21 '11 at 15:41
  • Doesn't this work: `std::ofstream myfile("test.txt", std::ios::binary); myfile.seekp(125, std::ios::beg); myfile.write(buf, n);`? This will seek to 125 and write from there, overwriting what was there originally. Make sure the file size is at least 125. – Kerrek SB Jun 21 '11 at 15:44
3

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.

Aleksey
  • 31
  • 1
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;
}`
PTT
  • 526
  • 7
  • 27