home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: Just what is so great about streams?
- Message-ID: <1992Nov12.175553.8218@taumet.com>
- Organization: TauMetric Corporation
- References: <1992Nov12.061942.16473@nuscc.nus.sg>
- Date: Thu, 12 Nov 1992 17:55:53 GMT
- Lines: 45
-
- tim@gold.iss.nus.sg (Tim Poston) writes:
-
- >This posting is a genuinely answer-seeking question,
-
- > what _is_ so great about streams?
-
- Streams are not the best solution for all I/O or formatting problems.
- Streams provide useful solutions to common I/O and formatting problems.
-
- Streams provide type safety, generalization of I/O, and user
- extensibility. These features are not available in C stdio.
-
- 1. Type safety: Printf and scanf provide no verification that the
- format codes match in number or type the parameters passed. This is
- frequent cause of program failures which are hard to find. More
- subtly, there are no formatting codes which portably correspond to
- the standard types size_t, ptrdiff_t, or wchar_t:
- size_t x; // is it sizeof int or sizeof long?
- scanf("%u", &x); // might work, might give wrong anwser
- scanf("%lu", &x); // might work, might trash the stack
- Portable code has to work around this problem.
-
- 2. Generalization of I/O: If 's' is (derived from) an ostream, you can write
- s << foo;
- without knowing or caring whether it is a file, a string, or some
- other kind of device. You have to use separate calls based on
- external information with stdio.
-
- 3. User extensibility: You can define your own "shift" operators for
- your own types, and do I/O without the need for special function calls:
-
- typedef ... T;
- ostream& operator<<(ostream&, const T&);
- istream& operator>>(istream&, const T&);
- ...
- extern T t;
- cout << t;
- cin >> t;
- The last two statements continue to work unchanged even if the type of
- T is changed. (You could get comparable functionality with overloaded
- I/O functions in C++ using stdio internally instead of streams.)
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-