What difference does it make whether a file is opened in text mode vs. binary mode? I seem to be finding the same results either way. When I write an int using write() the file contains the 4 bytes that make up the int. When I write the int using <<, the file contains the ascii codes for the digits that represent the int. The same things occur regardless of whether I use fout.open( "iotest.dat" ) or fout.open( "iotest.dat", ios::binary ). It seems that the only thing that matters is the output function, not the file type. Why?
Code:#include <iostream> #include <fstream> using namespace std; int main () { int intval1 = 1919006563; // this is what I should get back when the chars are put together int intval2, intval3; unsigned char buffer[4]; void* vp; cout << "First test - view contents of binary file written with unformatted output:\n"; ofstream fout( "iotest.dat", ios::binary ); if( fout == NULL ) { cout << "couldn't open for output\n"; return 1; } buffer[0] = (unsigned char)99; buffer[1] = (unsigned char)183; buffer[2] = (unsigned char)97; buffer[3] = (unsigned char)114; vp = buffer; fout.write( (const char*)vp, 4 ); vp = &intval1; fout.write( (const char*)vp, 4 ); fout.close(); ifstream fin( "iotest.dat", ios::binary ); if( fin == NULL ) { cout << "couldn't open for input\n"; return 1; } vp = &intval2; fin.read( (char*)vp, 4 ); cout << intval2 << endl; vp = buffer; fin.read( (char*)vp, 4 ); for( int i = 0; i < 4; ++i ) { cout << (int)buffer[i] << endl; } while( !fin.eof() ) { fin.read( (char*)vp, 1 ); if( !fin.eof() ) cout << (int)buffer[0] << " "; else cout << "reached eof"; } cout << endl; fin.close(); cout << "\nSecond test - view contents of binary file written with formatted output:\n"; fout.open( "iotest.dat", ios::binary ); if( fout == NULL ) { cout << "couldn't open for output\n"; return 1; } fout << intval1; fout << (unsigned char)99; fout << (unsigned char)183; fout << (unsigned char)97; fout << (unsigned char)114; fout.close(); fin.open( "iotest.dat", ios::binary ); if( fin == NULL ) { cout << "couldn't open for input\n"; return 1; } while( !fin.eof() ) { fin.read( (char*)vp, 1 ); if( !fin.eof() ) cout << (int)buffer[0] << " "; else cout << "reached eof"; } cout << endl; fin.close(); cout << "\nThird test - view contents of text file with unformatted output:\n"; fout.open( "iotest.dat" ); if( fout == NULL ) { cout << "couldn't open for output\n"; return 1; } buffer[0] = (unsigned char)99; buffer[1] = (unsigned char)183; buffer[2] = (unsigned char)97; buffer[3] = (unsigned char)114; vp = buffer; fout.write( (const char*)vp, 4 ); vp = &intval1; fout.write( (const char*)vp, 4 ); fout.close(); fin.open( "iotest.dat", ios::binary ); if( fin == NULL ) { cout << "couldn't open for input\n"; return 1; } cout << "Unformatted input: "; while( !fin.eof() ) { fin.read( (char*)buffer, 1 ); if( !fin.eof() ) cout << (int)buffer[0] << " "; else cout << "reached eof"; } cout << endl; fin.clear(); fin.seekg(0); cout << "Formatted input: "; while( fin >> buffer[0] ) cout << (int)buffer[0] << " "; cout << endl; fin.close(); cout << "\nFourth test - view contents of text file with formatted output:\n"; fout.open( "iotest.dat" ); if( fout == NULL ) { cout << "couldn't open for output\n"; return 1; } buffer[0] = (unsigned char)99; buffer[1] = (unsigned char)183; buffer[2] = (unsigned char)97; buffer[3] = (unsigned char)114; for( int i = 0; i < 4; ++i ) fout << buffer[i]; fout << intval1; fout.close(); fin.open( "iotest.dat", ios::binary ); if( fin == NULL ) { cout << "couldn't open for input\n"; return 1; } cout << "Unformatted input: "; while( !fin.eof() ) { fin.read( (char*)buffer, 1 ); if( !fin.eof() ) cout << (int)buffer[0] << " "; else cout << "reached eof"; } cout << endl; fin.clear(); fin.seekg(0); cout << "Formatted input: "; while( fin >> buffer[0] ) { cout << (int)buffer[0] << " "; } cout << endl; fin.close(); return 0; }



LinkBack URL
About LinkBacks


