Archived

This topic is now archived and is closed to further replies.

C vs C++ IO

This topic is 5872 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 Is it bad programming style to use the old C IO functions (printf, fopen, fclose, etc.) in an otherwise pure C++ project ? Are their any important differences (ie. performance) between the two ?

Share this post


Link to post
Share on other sites
The major difference between the two is that C++ IO is typesafe. When you do:

  
int a;
cin >> a;


The compiler knows that a is an int and selects the correct input mechanism. You can also overload the stream operators to support your own data types. Being object based, C++ IO is exception safe too - when an execption is thrown all iostreams (including file streams) are closed automatically.

With c style IO, you have a number of fixed types, and you have to tell the IO function the type of data it is working on. This used to cause a fair few errors (especially with scanf and friends) because its easy to get the format specifier wrong. In terms of performance, AFAIK c IO is a bit faster but thats because its much simpler and has fewer features.

From that, it looks like c++ IO is always the way to go, but I still use c IO every now and then. I find that for some jobs, like tabular output, c++ IO statements get very long and hard to read. Also, I have never been able to master true binary file IO in c++. Whatever I try, the input bytes (which are just raw byte values) get parsed as if they are characters. I`m sure its just because I don`t know the right way to do it, but I can`t afford a good iostreams reference and fread does all I need.

Share this post


Link to post
Share on other sites
@Krunk: got the same problem and no idea how to solve it, even if I open the streams in binary mode ASCII output is generated...

------------------------------------------------------------
"To a computer, chaos is just another kind of order."

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Krunk
In terms of performance, AFAIK c IO is a bit faster but thats because its much simpler and has fewer features.



This is not true. It depends totally on implementation. A poor implementation of iostream will be slow, but a good implementation should be just as fast c''s io.

Share this post


Link to post
Share on other sites
Anybody who''s seen me around here knows I''m a big-time proponent of C++. But I find the C++ streams extremely difficult to use. When it came down to crunch time a couple of weeks ago and I needed to get a file-modifying program out the door in about 6 hours, you bet your bippy I used the stdio file functions. Actually, I created inline wrappers to the stdio functions (seek, tell, read & write) and had the wrappers throw exceptions if they failed--this is the one thing I hate about stdio, you have to check for errors each time. Anyway, that''s my bit of shame for the day.

Share this post


Link to post
Share on other sites
quote:
Original post by Stoffel
But I find the C++ streams extremely difficult to use




I concur, streaming using the std c++ classes is a royal pain in the a$$. I prefer the good ole fashioned fopen/fclose for buffered and open/close for unbuffered.

Just because C++ has a std library doesn''t mean you have to use it. You are not violating anything if you don''t, but as Krunk said

quote:
Original post by Krunk
The major difference between the two is that C++ IO is typesafe.



To me it is 6 dozen of one and halfdozen of the other. Careless bugs on one side or ackward implentation on the other. That will probably get me flamed


D.V.

Carpe Diem

Share this post


Link to post
Share on other sites
Heh... ok, my thoughts:

The ''binary'' output for Iostreams just means ''don''t translate new lines''. It doesn''t mean write everything out as a binary representation. To do that, the best idea is to cast your data to chars and write them out like that. I think you might need to use the read and write members, rather than insertion/extraction operators, or indeed get/getline etc. Read the docs: it can be done.

As for awkwardness: I''ve never really had a problem with it. Probably cos I saw iostreams before I saw fopen etc. Type-safety is a big thing to me, as is not having to look up printf specification field stuff. I think it might well be a bit more bloated than stdio though.

Share this post


Link to post
Share on other sites
_lread() _lwrite() anybody? I primarily use those functions, I sometimes use the fstream objects, but it''s all down to what I feel like when I''m implimenting it. I''ve never used fread and fwrite much at all.

Share this post


Link to post
Share on other sites
quote:
Original post by Kylotan
The ''binary'' output for Iostreams just means ''don''t translate new lines''. It doesn''t mean write everything out as a binary representation. To do that, the best idea is to cast your data to chars and write them out like that. I think you might need to use the read and write members, rather than insertion/extraction operators, or indeed get/getline etc. Read the docs: it can be done.


I tried read/write and get/getline. The precise problem I`m having isn`t with newlines - its with escapes and backspaces. My program was trying to load a binary PPM image, which is basically a 24 bit raw bitmap with a minimal header. Whenever the colour value was 0x27, the stream turned bad and just gave me 0xFF`s. That made my lovely texture maps go all white .

I spent ages trying to solve it, but in the end I just cracked out stdio.h and it was fixed in no time.

Like I said, I`m sure it *can* be done with fstreams, but I have a solution that works perfectly well, so I dont need to read the iostream docs

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
This is not true. It depends totally on implementation. A poor implementation of iostream will be slow, but a good implementation should be just as fast c''s io.


This may be so, but every iostream implementation I''ve used must be poor because I''ve never seen a quick one.

Share this post


Link to post
Share on other sites
I assume that the various *nix have asycronous IO (I know Windows does), so that''d be the route I''d suggest for performance. It can be made portable with an abstract factory. But that''s alot of work. If you want quick&easy binary IO, fopen/fclose are your friends.

You should have classes and/or methods that perform the file IO, and they could/should provide exception safety and any other OO features you want.

Magmai Kai Holmlor
- Not For Rent

Share this post


Link to post
Share on other sites