Archived

This topic is now archived and is closed to further replies.

spg

ifstream or FILE*

Recommended Posts

spg    122
Which should I be using? Is there an advantage one way or another, assuming I am comfortable using both? -- Steve --

Share this post


Link to post
Share on other sites
evillive2    779
isn''t ifstream a wrapper for FILE*? or is it more?

I wrote a bunch of functions a while back based on others I had seen that used FILE* and when I moved to C++ I turned into a class. I have never thought to use ifstream because my own class does everything that I have wanted to do with files so far. I assume someday I will want more functionality but reading/writing a word (understands single/double quotes), integer, line, and special char terminated string to/from text files and reading/writing in binary is plenty for me right now. Am I really missing out by not using ifstream?

Share this post


Link to post
Share on other sites
stefu    120
quote:
Original post by Raloth
ifstream is the C++ way. Personally I find using FILE* easier and more straightforward.


ifstream is("filename.ext");
if (!is.is_open()) return false;

// What is so hard there? You don't even need to close it manually!

And look at here! Viola!!!

And if you are programming in C++, of course you use ifstream!!!

[edited by - stefu on August 14, 2003 2:50:51 AM]

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
evillive2
isn''t ifstream a wrapper for FILE*? or is it more?



C''s file routines may themselve just be wrappers around OS-specific file IO libraries.

Anyway, the source for the iostream classes are available, so in case of doubt, you can check for yourself how it is done on your particular OS+compiler+library platform.

quote:

Am I really missing out by not using ifstream?



Not necessarily missing much in the stream class itself, but you''re missing a lot of what has been built around it

- algorithms : you can read/write to streams as if they were STL containers.
- extensibility : adding support for new data types is much easier than trying to extend printf/fprintf. You can also add support for new types of stream (network sockets, GUI elements).
- stream polymorphism : functions using streams do not have to care whether the stream is doing console IO, file IO, or in-memory ''string'' IO or something else.

Adding support for a new data type makes it readable/writable on any stream (derived from the proper base stream class), whether standard or user-implemented, past, present or future.

For example, tokenizers that recognize quoted strings and more have been built - see boost::tokenizer

Finally, note that the high-level stream interface correspond to the fscanf/fprintf C interface. If you want lower-level fread/fwrite functionality, you can use the methods of the stream buffer each iostream object encapsulates ( accessed as a pointer through stream.rdbuf() )

quote:
Original post by stefu

And if you are programming in C++, of course you use ifstream!!!




So long as you are talking about standard ifstreams found in <iostream> and not the crappy old streams still rotting in <iostream.h>.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites