ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
In answer to the third question, the answer is that it's probably best not to. I think GNU libc++ implements iostream on top of stdio (which makes it somewhat slower), but I don't think you can guarantee that both libraries will work well together across all platforms/compilers. You could probably get away with using both at once, but it's just asking to be bitten by obscure bugs...
On the other hand, if you only use one or the other for each open file, there should be no problems.
Last edited by llama_meme; 11-01-2003 at 05:05 PM.
It's really dangerous to use both stdio and iostream in one single file. Imaging if you declare a variable with new (iostream) and destroy it with free (stdio), this can cause a serious problem. For an advanced c++ compiler, an variable declared with new should not be destroyed with free. Be carefully!!!
Personally, I prefer stdio, it gives you more freedom.
You can use stdio with C++ programs, but problems can occur when you mix iostreams and stdio in the same standard stream within a program. For example, if you write to both stdout and cout, independent buffering occurs and produces unexpected results. The problem is worse if you input from both stdin and cin, since independent buffering may turn the input into trash.
To eliminate this problem with standard input, standard output and standard error, use the following instruction before performing any input or output. It connects all the predefined iostreams with the corresponding predefined stdio FILEs.
Such a connection is not the default because there is a significant performance penalty when the predefined streams are made unbuffered as part of the connection. You can use both stdio and iostreams in the same program applied to different files . That is, you can write to stdout using stdio routines and write to other files attached to iostreams. You can open stdio FILEs for input and also read from cin so long as you don't also try to read from stdin.
Fei is correct, sorry I should have noted that it is safe to use the two libraries in the same application, but be very careful about using them on the same file. If you do require to use the two libraries on the same file make sure the line:
is called before any IO occurs (first line in mail is a good place ) And do *not* call ios::sync_stdio(false).
This will degrade performance somewhat so only use it if you need to..