Sign in to follow this  

Iostream of stdio's printf, scanf, etc.?

Recommended Posts

Hi, I know about the iostream standard library and the stdio. I was wondering whats the advantages/disadvantages of using iostream over stdio's printf, scanf, etc. Noob question :) Thanks, Mitchell H.

Share this post

Link to post
Share on other sites
In case you were not aware, the reason there is two different ways of going this is that printf/scanf/etc are the C style input/output and streams are the C++ style.

One of the major advantages of streams is that it makes it easier for you to make sure you dont write data past the end of arrays.

For example:

// c style
char mybuf[32];
scanf("%s", mybuf); // what happens if someone enters a really long string?

// c++ style
string mybuf;
cin >> mybuf; // however long the input, it will always be safe

Streams also take care of converting to the correct data type for you. A common mistake people make using printf/scanf is to use the wrong token type in their formatting string. With streams the datatype of the variable being used determines how it is converted.


Share this post

Link to post
Share on other sites
Type safety.

Using printf you have to tell it what the data is that you are passing it, by giving it a formating string. This is evaluated at runtime. If there is an error in the string it can mean the data gets messed up.

Iostream checks types at compile time so you avoid making those mistakes.

If you create your own classe types you can make them work with the stream concept. Then anywhere you want to write your class object to a file (for example), you use the stream and it 'just works'.

If you were wanting to use printf to output your new object type then you'd have to write the format string everytime you did that, or create a function that had to be called, or have a lookup function to return the correct format string, or something. But whatever, it's a lot more work and error prone than using streams.

Share this post

Link to post
Share on other sites
Type safety.
double x = 0;
double y = 0;
fprintf(file, "Position: %d, %d", x, y); // Oops. %d is decimal, not double!
fileStream << "Position: " << x << ", " << y; // You just can't get this wrong

Also it makes printing user defined classes easier.
class Quaternion
// implementation details

std::ostream& operator<<(std::ostream& stream, const Quaternion& quaternion)
stream << quaternion.toString();

fprintf(file, "%s", quaternion.toString()); // have to remember toString() function each time
fileStream << quaternion; // toString() called for you

Streams can be quite a bit slower even now depending on the compiler you use (The Borland compiler seems particularly bad at this), but this is implementation dependant and nothing to do with the underlying capabilities of streams. The performance of std::cout etc. can be increased by telling them not to sync with the stdio functions, but I forget what function does this.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this