• Advertisement
Sign in to follow this  

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

This topic is 4841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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.

Alan

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.

Enigma

Share this post


Link to post
Share on other sites
Thank you guys. I was always planning on using the iostream library since it did type conversions and the like, but for reference i wanted to know what the difference was.

Thanks again!

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
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.


See this thread.

Share this post


Link to post
Share on other sites
Thanks, that's the thread I was thinking of. Just couldn't find it.

Enigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement