Page 1 of 1

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

Posted: Sat Sep 17, 2011 6:41 am
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

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

Posted: Sat Sep 17, 2011 9:12 am
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

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

Posted: Sat Sep 17, 2011 3:12 pm
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.

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

Posted: Sun Sep 18, 2011 4:19 am
by Pornomag
Thank you very much guys, I'll probably use C-Style FILE I/O for formatted files...

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

Posted: Mon Sep 19, 2011 12:41 pm
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.

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

Posted: Mon Sep 19, 2011 1:07 pm
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.