• Advertisement
Sign in to follow this  

Strange file writing

This topic is 4519 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why do I get 3 bytes into the file when I explicitly wrote 2? And why does this only appear if i give num a value of 10? See code below. File data is: '0d 0a 00'. Shouldn't it be: '0a 00'?
// C++ version
ofstream file;

file.open( "file001", ios::out );

unsigned short num = 10;
file.write( (char*)&num, 2 );

file.close();


// C version

FILE *pFile;

pFile = fopen( "file001", "w" );

unsigned short num = 10;
fwrite( (void*)&num, 2, 1, pFile );

fclose( pFile );


Share this post


Link to post
Share on other sites
Advertisement
Open your file in binary mode. You're getting newline translation noise.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement