News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

C++ Tips: IO - Mixing streams and standard IO

Started by ben2ong2, October 06, 2006, 11:14:10 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

ben2ong2

RESPONSE: You are not allowed to view links. Register or Login (Steve Clamage), 2 Sep 94

[...]

1. You can mix stdio and iostreams in the same program using different files
with no problems. E.g.
   FILE * file1 = fopen("data1.dat", "rw");
   fstream file2("data2.dat");

2. You can mix output to stdout and cout in the same program, but on most
systems you must first invoke the static member function
   ios::sync_with_stdio();
Example:
   main()
   {
      ios::sync_with_stdio();
      cout << ...
      printf(...);
      cout << ...
      printf(...);
      ...
   }
You must invoke the sync function before attempting any output.

3. Other forms of mixing iostreams and stdio functions to the same file
(other than cout/stdout cerr/stderr) are unlikely to work unless you
go to some extra trouble:
   a. Make the stream unit-buffered, and
   b. Flush the stdio file before every stream output.

Some iostream implementations, notably the one from Gnu, have no special
restrictions on mixing iostreams and stdio, since they use the same
data structures. The C++ Standard version of iostreams will mandate
mixing of output on cout/stdout and cerr/stderr without taking any special
action. Most current implementations have the restrictions I have noted.

What about input? Well, if two objects (an istream and a FILE) are each
buffering input independently, it is pretty hard to synchronize them.
It is very unlikely to work.




You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login