home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: <stdio.h> and <iostream.h>
- Message-ID: <1992Sep2.164351.11321@taumet.com>
- Organization: TauMetric Corporation
- References: <1SEP199214472267@cnsvax.uwec.edu>
- Date: Wed, 2 Sep 1992 16:43:51 GMT
- Lines: 54
-
- tannerrj@cnsvax.uwec.edu (Power = 486 + HST Dual Standard) writes:
-
- >I am trying to put a text windowing environment on a C++ program
- >that I have created. The text windowing environment is DFLAT from
- >DDJ magazine. The problem is DFLAT is ANSI C using stdio.h and
- >my program is a C++ program with iostream.h. I have read that there
- >are potential dangers in mixing the two. Any suggestions? Thanks.
-
- The potential problem in mixing iostreams and stdio on the same
- file is the separate buffering. For example, try this:
- #include <iostream.h>
- #include <stdio.h>
- main()
- {
- cout << "Hello ";
- printf("world\n");
- return 0;
- }
- On many systems, the output from printf appears first.
-
- There are two solutions.
-
- 1. Call the static member function ios::sync_with_stdio() before doing
- any I/O. This forces all I/O using the standard streams to go
- via stdio calls. This I/O is unbuffered and very inefficient.
- (It affects only I/O on cin, cout, cerr, and clog.)
- #include <iostream.h>
- #include <stdio.h>
- main()
- {
- ios::sync_with_stdio(); // synchronize stdout and cout
- cout << "Hello ";
- printf("world\n");
- return 0;
- }
-
- 2. Use only stdio functions on the standard streams from the C++ code.
- This ensures you will not have any unnecessary inefficiencies and
- no synchronization problems. You can use both stdio and iostreams
- safely, but on different files:
- #include <stdio.h>
- #include <fstream.h>
- main()
- {
- ofstream outfile("myfile.dat");
- printf("Hello world\n"); // to standard output
- outfile << "Hello world" << endl; // to iostream file
- return 0;
- }
-
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-