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

Started by
4 comments, last by Allmight 18 years, 11 months ago
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?
Advertisement
Some would disagree, but I like the C io functions better. It's very simple, its just something like 4 functions and a FILE handle.

C++ freaks would say that these C functions lack security, but this is rarely a serious matter.
The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group
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.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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.
I like the C++ functions quite a bit, although the C ones are alright.

In C++ it's as simple as

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


Bye!

[Edited by - thannett on May 25, 2005 6:42:07 PM]
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.

Ex:
FILE file;file = fopen( "filename", "w" );fprintf( file, "Hello World" );fclose( file );
-------------------------------------------------Founder and DirectorAllSoft Studios

This topic is closed to new replies.

Advertisement