Output file bigger than real data

Hello,

I have a char*, named 'buffer', loaded from a file and processed. After writing it to another file I opened it with some hex editor and found that the output file is bigger than the buffer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    ifstream section;
    int length;

    section.open ("sec54.map", ios::in);
    section.seekg(0, ios::end);

    length = section.tellg();

    section.seekg(0, ios::beg);

    char *buffer = new char[length];

    //[load and processing]

    ofstream ofile;
    ofile.open ("Nsec54.map", ios::out);
    ofile.seekp(0, ios::beg);

    cout << "Length: " << length << "bytes\n"; //Output: "Length: 15600 bytes"

    for (int i = 0; i < length; i++) {
         ofile.put(buffer[i]);
    }


Looking for bugs I tried with ofile.put(buffer[0]) and ofile.put(buffer[length]), for example, and the resulting file is ok (filled with the same value). But with buffer[ i ] it has 56 extra bytes. The read function with length as second parameter has the same behaviour.

How is this possible?

Thank you!
Last edited on
You're not opening the file as binary, so it's very likely that every instance of the byte value 0A is being converted to the byte string 0D0A.

By the way, I would replace the loop on lines 21-23 with a call to write: ofile.write(buffer,length);
Oh I tried that, but you made me realize I forgot to open the output file as binary too. It's working now!

Thank you
Topic archived. No new replies allowed.