home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: Binary File Streams (iostream derived)
- Message-ID: <1992Nov23.211711.4649@taumet.com>
- Keywords: Binary Streams
- Organization: TauMetric Corporation
- References: <pbray.722479093@firebird>
- Date: Mon, 23 Nov 1992 21:17:11 GMT
- Lines: 39
-
- pbray@firebird.newcastle.edu.au (Peter Bray) writes:
-
-
- > Having just finished looking thru two book and the AT&T C++ v3.01
- >manual pages, I was wondering if there was a simple interface to binary file
- >streams, or whether one must open the file as usual and just use the
-
- > ostream& write( const unsigned char* ptr, int n )
-
- >interface and develop member I/O functions (wrappers), for each class for which
- >one desires binary I/O.
-
- There are two considerations: binary versus text files, and I/O
- conversions versus no conversion.
-
- Systems which distinguish between text and binary files have a flag
- you can pass to the fstream constructor or open() function to tell
- it to use binary rather than text mode. Example:
- ofstream outfile("data.dat", ios::out|ios::bin);
- (The flag is usually called "bin" or "binary" when it exists.)
-
- The read() and write() functions (along with get() and put()) are the
- only iostream functions which do no text conversion. You can't use
- the supplied "shift" operators for binary I/O. Additionally, the
- "shift" operators are not virtual, so replacing them with binary
- versions is a bit risky. You can write your own operators for your
- classes that do binary I/O, however:
- class myclass { ... };
- ostream& operator<<(ostream& o, const myclass& c)
- {
- o.write(c.data, sizeof(c.data));
- }
- myclass dat;
- ofstream outfile("data.dat", ios::out|ios::binary);
- outfile << dat;
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-