Should I use C-Style FILE I/O instead of C++ Style?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Pornomag
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 41
Joined: Tue Jun 21, 2011 5:39 am
Programming Language of Choice: C++

Should I use C-Style FILE I/O instead of C++ Style?

Post by Pornomag »

Should I use the C-Style I/O methods for files? Or should I stick to fstream, only saying this because I want it to be easily read, like for example in my game for a window I would have the following:

Code: Select all

Screen Height= 640
Screen Width= 480
Bits Per Pixel= 32
Window Title= Game
User avatar
dr-snipe
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Sun Dec 19, 2010 10:09 pm
Programming Language of Choice: C++, Java, PHP
Contact:

Re: Should I use C-Style FILE I/O instead of C++ Style?

Post by dr-snipe »

I personally have always used fstream and never had a problem with it. But a quick Bing search (yes I use Bing) that led me to an interesting discussion: http://www.codeguru.com/forum/showthread.php?t=383112
Live-Dimension
Chaos Rift Junior
Chaos Rift Junior
Posts: 345
Joined: Tue Jan 12, 2010 7:23 pm
Favorite Gaming Platforms: PC - Windows 7
Programming Language of Choice: c++;haxe
Contact:

Re: Should I use C-Style FILE I/O instead of C++ Style?

Post by Live-Dimension »

There isn't any massive difference - whatever is easier for you to code. I only use file code like this inside classes which handle loading/saving and formatting the data. The program askes the class for xyz data and the class provides it - the rest of the program has no knowledage about wether fstream or c-style files, or read from memory, or the network, etc.
Image
Pornomag
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 41
Joined: Tue Jun 21, 2011 5:39 am
Programming Language of Choice: C++

Re: Should I use C-Style FILE I/O instead of C++ Style?

Post by Pornomag »

Thank you very much guys, I'll probably use C-Style FILE I/O for formatted files...
midix
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Sat Sep 10, 2011 1:02 pm

Re: Should I use C-Style FILE I/O instead of C++ Style?

Post by midix »

I personally have once had a major problem with fstream.

As I live in a country where people use computers with russian and latvian languages, I had to support Unicode so users can load files with Unicode characters in file names.

I started using Microsoft's wfstream which works with Unicode files. The application data was stored in binary format in those files. So I set the file type as binary with ios::binary flag.

And then while loading data from a file, I had an exception at the very first data chunk. I do not remember the exact message of that exception, but while looking for solution, I found out that wfstream (even in binary mode :shock2: ) is trying to process data as Unicode characters, and if the byte sequence occurs to be invalid Unicode, wfstream throws an exception.

I did not inpsect further, maybe there were some additional flags to fix this problem, but I went more usual way: I used wfopen and processed my Unicode-named files in the old good C way.
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Should I use C-Style FILE I/O instead of C++ Style?

Post by TheBuzzSaw »

Lemme share an insight from where I work... "the real word" if you will... XD

Our C++ code base uses lots of the scanf and printf variants for parsing data. Over time, these calls started crashing the program. Basically, we have stuff like this:

Code: Select all

sscanf(dataBuffer, "%4d%2d%2d", &year, &month, &day);
The problem with this kind of code is that it is not type-safe. It makes general assumptions about the parameters based on the format string; scanf does not really know that year/month/day are integers. It just assumes you were smart enough to give it the right stuff. In the case of our code, if the incoming data is malformed or incomplete (or some lazy engineer put the wrong number of parameters), data was exiting the program's memory boundaries and blowing stuff up. (Michael Bay programming! WOOOOO!)

I now much prefer doing this.

Code: Select all

std::stringstream ss;
ss << formattedYear << ' ' << formattedMonth << ' ' << formattedDay;
ss >> year >> month >> day;
Sure, it takes a little bit more setup (depending on how the data is coming in), but this prevents all the memory-stomping that was going on in the earlier code snippet. So, coming back to the topic's question: should you use cstdio or fstream? Sometimes, it doesn't matter. One thing I like about C# is that file operations have mostly been reduced to single line commands: File.ReadAllBytes opens a file, loads the entire file into a byte array, and closes the file. I've gotten into that habit in C++ (assuming the file is not ridiculously huge): open the file, load the entire file into a char array, close the file, and perform all operations on the array. So, in that respect, it really doesn't matter a whole lot whether I use fopen of ifstream. (I even ended up using fopen in some of my Android code because STL was not available/stable yet.)

However...

I think in the long run, it is better to use fstream. Your I/O will be type-safe and have a lower chance of blowing up in your face. Especially if you still plan on reading directly from the stream (instead of loading it into a char array first), you will have the ability to read out words/lines at a time, use <iomanip>, etc.
Post Reply