I’ve just run across some C++ code which uses a FILE* to manipulate a file stream using the “f” functions (fopen, fseek, fread, etc). I believe that these are provided by standard header cstdio.
Is this considered a dated or bad practice in modern C++ code? I ask because I also see that you can get/set the position of the stream of an ifstream object using setg and tellg and I’d like to know what the advantage is to doing it this way. Is this an “old habits die hard” C programmer way of manipulating the stream or is there still a valid reason to use FILE* and the “f” functions in modern C++ code?
They are there for C compatibility, although there’s nothing stopping you from using them in a modern C++ program.
Some people tend to prefer C streams because either they dislike the C++ streams (which are arguably quite a mess in many respects), or they think the C streams are faster. This last point was valid on many implementations few years ago, but on modern implementations C++ streams usually tend do be somewhat faster (this statement is based on some benchmarks I did on g++, YMMV).
Still, there’s an important problem with C streams: since they are C stuff and aren’t implemented in terms of classes, you must be extra careful when using them together with exceptions, otherwise you may leak file handles; this problem isn’t relevant with C++ file streams since they are encapsulated in classes, whose destructor is called when an exception causes a stack unwind, so file handle leaking is prevented.