Sign in to follow this  

What should I used for file I/O in my games? C or C++?

Recommended Posts

Ekim_Gram    418
Yeah, I tried to be as direct as I could in my topic title. Here's my situation: I've been working on a Tetris clone (Block-O-Rama) for the past two months or so. I've been using mostly C++ for this except for some string stuff with constantly updating values such as the score and level. The game itself is done, looks very old school like 8-bit NES and I'm quite happy with it. Graphics aren't my thing so I wasn't too worried about that. It has now come time where I add the finishing touches, namely the in-game credits and high score board. Out of the two, I'm chosing the high score board first. I know how I'm going to go about creating such a thing, it doesn't seem like such an arduous task. The only problem is that when learning C++ I never really got too in depth in file I/O. I'm not a master with it and if I were to do anything, I would have to go read a tutorial or chapter to get what I need done. That doesn't bother me. What does bother me, though, is whether I should be using C or C++ functions for this. Which would be better for the task at hand? Which would be faster, easier, cleaner?

Share this post

Link to post
Share on other sites
Agony    3452
I would expect that speed would be totally irrelevant for this feature; no matter how horribly you misuse any file I/O functions (C or C++), writing the high score file will still seem like a more or less instant action.

As for easier, if you're already familiar with printf, then C file I/O will probably seem easy. If you're already familiar with std::cout, then C++ file I/O will probably seem easy. If you're familiar with both, it'll be a toss up between which one you like more. Me, I'm a C++ person. I like streams the way C++ has them set up, with overloaded << and >> operators and such. I'm definitely comfortable with printf()-like functions, but they simply don't look as intuitive to me.

Cleaner? I'd say C++ for this one. Objects clean up after themselves. That alone is nicer than having to remember to call fclose(). It's minor, but there are a lot of little things like that.

If nothing else, though, I'd suggest doing C++ just so that you'll be familiar with it in case you ever are required to use it in the future for some reason.

Share this post

Link to post
Share on other sites
Telastyn    3777
It probably doesn't matter, since you're likely to wrap the file IO functions away in their own class anyways.

Personally, I tend to use the C++ stuff. Just doing an fstream.good() is [to me] nicer than making sure C streams are still good.

Share this post

Link to post
Share on other sites
thannett    214
I like the C++ functions quite a bit, although the C ones are alright.

In C++ it's as simple as

char letter;
ifstream fin;"/*filename*/");
fin >> letter;
ofstream fout;"/*filename*/");
fout << "La La, Apples and Oranges!";
fout.close(); //or just flush if you only want to save


[Edited by - thannett on May 25, 2005 6:42:07 PM]

Share this post

Link to post
Share on other sites
Allmight    206
I prefer the same as above mentioned. It is easy to open a file, and you can use all fprint and sprint functions to format the text.


FILE file;

file = fopen( "filename", "w" );

fprintf( file, "Hello World" );

fclose( file );

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