delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/11/07/18:35:28

From: deepblack AT geocities DOT com (Luis Coelho)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: ios::binary ignored by GCC - HELP!
Date: Sat, 07 Nov 1998 23:24:26 GMT
Organization: Instituto Superior Tecnico
Lines: 33
Message-ID: <3644d428.2862999@news.ist.utl.pt>
References: <fycflpbqrpbz DOT f2134e0 DOT pminews AT nntp DOT generation DOT net>
NNTP-Posting-Host: ppp14.ist.utl.pt
X-Trace: ci.ist.utl.pt 910477132 11393 (None) 193.136.102.46
X-Complaints-To: news AT news DOT ist DOT utl DOT pt
X-Newsreader: Forte Free Agent 1.1/32.230
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019