0

I am trying to write to an existing blank spaced binary file of 1 MB in size via C++, at a particular position.

ofstream of( filename, ofstream::binary );
of.seekp(1600, of.beg);
of.write(arr, 1024);
of.close();

The blank file is created by writing this buffer to file before:

char *buffer = (char*)malloc(size);     
memset(buffer, ' ', size);

But this code just truncates the entire blank space file of 1MB and writes this 1KB content and the size of the file becomes 1KB.

Tried using the app as well as ate mode too. When I tried app mode like this:

ofstream of( filename, ofstream::binary | ofstream::app );
of.seekp(1600, of.beg);
of.write(arr, 1024);
of.close();

It adds the content to the end of the file. Thus the file becomes of size 1MB+1KB.

What I am trying to do is overwrite a 1KB data(at present it is blank space) in the file with some data. So that it won't change the file size or any other data.

Where am I doing it wrong ?

4
  • 5
    I can't be the only person that is curious what a "blank spaced binary file" is. I just can't be... Commented Oct 25, 2013 at 18:38
  • sorry if i used the wrong term. I included the code above. Please check Commented Oct 25, 2013 at 18:43
  • Ah.. ok. so it is a file that leads with a predetermined bytes equivalent to a platform-dependant space (ascii 0x20, for example, or ebcdic 0x40). That makes a bit more sense. Thanks. Commented Oct 25, 2013 at 18:51
  • 1
    For what it's worth, I find that mmap (there is a similar mechanism for Windows) can be very convenient when reading/updating data at random offsets in an existing file. IMHO code which uses it tends to be more legible than code that seeks around using streams. Commented Oct 25, 2013 at 18:52

1 Answer 1

3

Use

ofstream of( filename, ofstream::binary | ofstream::in | ofstream::out)

to open the binary file for update.

See C++11 Table 123 - File Open Modes (in 27.9.1.4/3):

Table 132 — File open modes

 ios_base flag combination          stdio 
binary  in   out  trunc  app       equivalent
---------------------------------------------------
              +                      "w"    truncate/create for writing
              +           +          "a"    append - writes automatically seek to EOF
                          +          "a"    
              +     +                "w"    
        +                            "r"    open for read
---------------------------------------------------
        +     +                      "r+"   open for update (reading and writing)
        +     +     +                "w+"   truncate/create for update
        +     +           +          "a+"   open or create for update - writes seek to EOF
        +                 +          "a+"
---------------------------------------------------
  +           +                      "wb"   All same as above, but in binary mode
  +           +           +          "ab"
  +                       +          "ab"
  +           +     +                "wb"
  +     +                            "rb"
---------------------------------------------------
  +     +     +                      "r+b"
  +     +     +     +                "w+b"
  +     +     +           +          "a+b"
  +     +                 +          "a+b"
Sign up to request clarification or add additional context in comments.

2 Comments

Because if you don't the file will be truncated.
Michale Burr, that was brilliant! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.