home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13212 < prev    next >
Encoding:
Text File  |  1992-09-02  |  2.0 KB  |  65 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: <stdio.h> and <iostream.h>
  5. Message-ID: <1992Sep2.164351.11321@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <1SEP199214472267@cnsvax.uwec.edu>
  8. Date: Wed, 2 Sep 1992 16:43:51 GMT
  9. Lines: 54
  10.  
  11. tannerrj@cnsvax.uwec.edu (Power = 486 + HST Dual Standard) writes:
  12.  
  13. >I am trying to put a text windowing environment on a C++ program
  14. >that I have created. The text windowing environment is DFLAT from 
  15. >DDJ magazine. The problem is DFLAT is ANSI C using stdio.h and
  16. >my program is a C++ program with iostream.h. I have read that there
  17. >are potential dangers in mixing the two. Any suggestions? Thanks.
  18.  
  19. The potential problem in mixing iostreams and stdio on the same
  20. file is the separate buffering.  For example, try this:
  21.     #include <iostream.h>
  22.     #include <stdio.h>
  23.     main()
  24.     {
  25.         cout << "Hello ";
  26.         printf("world\n");
  27.         return 0;
  28.     }
  29. On many systems, the output from printf appears first.
  30.  
  31. There are two solutions.
  32.  
  33. 1.  Call the static member function ios::sync_with_stdio() before doing
  34.     any I/O.  This forces all I/O using the standard streams to go
  35.     via stdio calls.  This I/O is unbuffered and very inefficient.
  36.     (It affects only I/O on cin, cout, cerr, and clog.)
  37.     #include <iostream.h>
  38.     #include <stdio.h>
  39.     main()
  40.     {
  41.         ios::sync_with_stdio(); // synchronize stdout and cout
  42.         cout << "Hello ";
  43.         printf("world\n");
  44.         return 0;
  45.     }
  46.  
  47. 2.  Use only stdio functions on the standard streams from the C++ code.
  48.     This ensures you will not have any unnecessary inefficiencies and
  49.     no synchronization problems.  You can use both stdio and iostreams
  50.     safely, but on different files:
  51.     #include <stdio.h>
  52.     #include <fstream.h>
  53.     main()
  54.     {
  55.         ofstream outfile("myfile.dat");
  56.         printf("Hello world\n");        // to standard output
  57.         outfile << "Hello world" << endl;    // to iostream file
  58.         return 0;
  59.     }
  60.  
  61. -- 
  62.  
  63. Steve Clamage, TauMetric Corp, steve@taumet.com
  64. Vice Chair, ANSI C++ Committee, X3J16
  65.