ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Alex
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.
Code:
ios::sync_with_stdio();
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:
ios::sync_with_stdio()
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..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.