0

I am using fstream to write an integer to binary file.

int main(){

fstream f1;
int num = 2, num2 = 0;

f1.open("dat1", ios::app | ios::out | ios::in | ios::binary);

f1.write((char *)num, sizeof(int));
f1.seekp(0);
f1.read((char *)num2, sizeof(int));

cout << num2;

}

The problem is on line f1.write. I can write to binary file an array, but when I try to write just one block of int it gives me an error:

Unhandled exception at 0x522C7EA6 (msvcp120d.dll) in Project.exe: 0xC0000005: Access violation reading location 0x00000002.

I don't understand what the problem is.

4 Answers 4

5

You need to cast the address of num to char*, not num itself. In fact, the behaviour of using the result of (char*)num is undefined which explains the crash.

Use f1.write((char *)&num, sizeof(num)); instead. Change f1.read similarly.

I've also changed the sizeof argument: I prefer that style as it future-proofs you against type changes. Moving on, you'll need to consider endianness if you're writing and reading in different platforms (e.g. Windows and Linux).

Sign up to request clarification or add additional context in comments.

Comments

4

You need this:

f1.write((char *)&num, sizeof(int));
f1.seekp(0);
f1.read((char *)&num2, sizeof(int));

write() and read() want a pointer to where your data is.

int num = 2;

Using (char *)num converts num to a pointer pointing at address 0x00000002.


Also, when working with binary files (other than testing) it is always best to include a header, which should at least contain the following:

  • A Magic Number (usually 4 bytes), so the exact file type can be determined or checked regardless of what the extention might be.

  • A version number, which will allow you to extend the format and structure of your file in the future without breaking your current format.

  • As others already stated, a Byte Order Mark which will allow your data to be read in correctly on all platforms (Little-endian / Big-endian).

Comments

2

The line should actually be:

f1.write((char *)&num, sizeof(int));

You need an address as an argument to the write function. Check out http://www.cplusplus.com/reference/ostream/ostream/write/.

1 Comment

Also one needs to consider the byte oder in order to use the file in a platform independent manner if needed.
0

write want's a array. This means the start address. (char *)num this converts the number to a address and will point to a point you don't want. Use &num to get the address of num. Maybe make a cast with (char*) too.

Comments

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.