FAQ 36.07 Can C IO ( iostream ) be mixed with C IO ( stdio.h )?

FAQ 36.07 Can C++ I/O (<iostream>) be mixed with C I/O (<stdio.h>)?

graphics/new_icon.gif

Yes, but be careful.

<iostream> and <stdio.h> can be used in the same program. The easiest way to mix them is to make sure that no single file is manipulated using both <iostream> routines and <stdio.h> routines.

If any given file needs to be manipulated by both <iostream> routines and <stdio.h> routines, special considerations must be taken into account. Make sure that ios::sync_stdio(false) has not been called. If it has then call ios::sync_with_stdio() as shown.

 #include <iostream> #include <cstdio> using namespace std; int main() {   ios::sync_with_stdio();                            <-- 1   // ... } 

(1) No I/O should occur before this line

Note that this synchronization can degrade I/O performance, so it should be used only if <iostream> routines and <stdio.h> routines are manipulating the same file. For example, synchronization is needed if the program reads from both cin and stdin, or if it writes to both cout and stdout. But if <iostream> routines and <stdio.h> routines are not manipulating the same file, synchronization is unnecessary and should not be used.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net