Mail Archives: djgpp/1998/11/07/18:35:28
On Fri, 06 Nov 1998 20:36:14 -0500 (EST), "sl"
<sl AT psycode DOT com DOT REMOVE_THIS> uttered the following words:
> When I open files in ios::binary mode, it is ignored by GCC. For
>example, writting "7" to a file opened with the binary flag should write the
>7th ASCII character. Instead, the file contains the text-character "7".. How
>does one go about fixing this?
>
>Gili
Are you using something such as
ofstream fout("filename.dat", ios::binary);
int the_int = 7;
fout << the_int << 42;
?
operator<< *Always* outputs a character representation of its
second argument. To output integrers in binary, you must use
write(). So the example above becomes:
fout.write(static_cast<char*>(&the_int),sizeof(int));
Hope that helped,
Luís Coelho.
*******
Translations: Any combination between German, English & Portuguese.
Contact me at deepblack AT geocities DOT com
*******
C++ Programming Language, 3rd Ed. by B. Stroustrup. My exercise answers at:
http://www.geocities.com/SiliconValley/Way/3972/index.html
- Raw text -