简体   繁体   中英

manipulating a stream via FILE*

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.

AFAIK, in C++, the correct way is to use ifstream . cstdio , as it name indicates, is for C compatibility only.

Both are correct though.

For C++ the iostream library should be used in most cases.

The iostream library gives out in and output in a general way, and offers type safety and is less error prone than the cstdio . Also the iostream library offers extensibility, allowing you to inherit and overload to make your own types streamable in addition to the standard types.

There are however some people that thinks that the iostream library are overly verbose, also some use the cstdio library because it is most familiar to them.

Some valid points exists to use the cstdio though. Most iostream implementations are slower performance wise than the cstdio and there have conducted tests that show that a cout is slower than a printf .

So if speed is critical, or it is a mixed c++ and c code base, that might be an argument for chosing the cstdio library. Otherwise the standard io for c++ is iostream

In c++ you often prefer operating on streams. Makes life a bit easier when switching the file to a network stream (or any other kind of stream).

cstdio is, as stated, manly for c-compatibility but also the path to direct access and is still often found in code where people dislike the overhead of streams.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM