LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-01-2003, 04:33 PM   #1
jpc82
Member
 
Registered: Oct 2001
Distribution: Gentoo
Posts: 140

Rep: Reputation: 15
iostream, or stdio?


when programming a C++ program which should you use stdio, or iostream?

I'm mostly used to stdio from C, but I was wondering if it would be worth it to learn iostream.

Also, is it OK to use both lib's in the same program?
 
Old 11-01-2003, 05:04 PM   #2
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
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.
 
Old 11-01-2003, 06:36 PM   #3
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
jpc82,

Yes, I would learn iostreams, they are quite powerful, and less error prone.

Yes you can feel free to use any combination of stdio and iostream functionality. It is considered sane!

I'd suggest you include <cstdio> though, not <stdio.h> and <iostream> not <iostream.h> etc..

Cheers
 
Old 11-02-2003, 04:08 AM   #4
fei
Member
 
Registered: Jun 2003
Distribution: Ubuntu, Debian
Posts: 40

Rep: Reputation: 15
Re:

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.
 
Old 11-02-2003, 07:44 AM   #5
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
This is a useful summary, from http://www.ictp.trieste.it/~manuals/...ream.doc.html:

Quote:
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.
 
Old 11-02-2003, 05:05 PM   #6
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
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..
 
Old 11-02-2003, 06:39 PM   #7
jpc82
Member
 
Registered: Oct 2001
Distribution: Gentoo
Posts: 140

Original Poster
Rep: Reputation: 15
Thanks for all the input.

I guess I'll give iostream a try, and just spend the time learning how to do everything.
 
Old 11-05-2003, 08:10 PM   #8
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Quote:
Such a connection is not the default
Hmmm, I am not sure this website is correct.

If it was the first call to sync_with_stdio() would return false but the first call returns (or should return) true.

The reference, should anyone care, is 27.4.2.4 of the ISO standard.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
stdio.c cigarstub Programming 12 12-19-2014 06:39 AM
g++ compiler and iostream.h BACTRATE Mandriva 5 07-26-2009 04:12 AM
iostream not found krsnachaitanya Linux - Software 4 02-10-2005 06:47 AM
where is iostream invisibleghost Programming 2 10-24-2004 01:30 PM
curses and iostream irfanhab Programming 3 05-10-2004 10:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration