home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16794 < prev    next >
Encoding:
Text File  |  1992-11-23  |  1.8 KB  |  51 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Binary File Streams (iostream derived)
  5. Message-ID: <1992Nov23.211711.4649@taumet.com>
  6. Keywords: Binary Streams
  7. Organization: TauMetric Corporation
  8. References: <pbray.722479093@firebird>
  9. Date: Mon, 23 Nov 1992 21:17:11 GMT
  10. Lines: 39
  11.  
  12. pbray@firebird.newcastle.edu.au (Peter Bray) writes:
  13.  
  14.  
  15. >    Having just finished looking thru two book and the AT&T C++ v3.01
  16. >manual pages, I was wondering if there was a simple interface to binary file
  17. >streams, or whether one must open the file as usual and just use the 
  18.  
  19. >    ostream& write( const unsigned char* ptr, int n )
  20.  
  21. >interface and develop member I/O functions (wrappers), for each class for which
  22. >one desires binary I/O.
  23.  
  24. There are two considerations: binary versus text files, and I/O
  25. conversions versus no conversion.
  26.  
  27. Systems which distinguish between text and binary files have a flag
  28. you can pass to the fstream constructor or open() function to tell
  29. it to use binary rather than text mode.  Example:
  30.     ofstream outfile("data.dat", ios::out|ios::bin);
  31. (The flag is usually called "bin" or "binary" when it exists.)
  32.  
  33. The read() and write() functions (along with get() and put()) are the
  34. only iostream functions which do no text conversion.  You can't use
  35. the supplied "shift" operators for binary I/O.  Additionally, the
  36. "shift" operators are not virtual, so replacing them with binary
  37. versions is a bit risky.  You can write your own operators for your
  38. classes that do binary I/O, however:
  39.     class myclass { ... };
  40.     ostream& operator<<(ostream& o, const myclass& c)
  41.     {
  42.         o.write(c.data, sizeof(c.data));
  43.     }
  44.     myclass dat;
  45.     ofstream outfile("data.dat", ios::out|ios::binary);
  46.     outfile << dat;
  47. -- 
  48.  
  49. Steve Clamage, TauMetric Corp, steve@taumet.com
  50. Vice Chair, ANSI C++ Committee, X3J16
  51.