From: Sadanand on
hi all,
I am bit confused about usage of ifstream and fopen ( old C-Style),
When exactly I should use ifstream and when to use old C-Style?
Are there any advantages of using ifstream.
As much I know ifstream in C++ is bit cleaner, more type safe. Are
there any other advantages?
Is ifstream usage is faster than old C-Style in C++?

Please let me know the exact difference

Thanks & Regards,
Sadanand

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mickey on
On Feb 15, 1:11 pm, Sadanand <steggi...(a)gmail.com> wrote:
> hi all,
> I am bit confused about usage of ifstream and fopen ( old C-Style),
> When exactly I should use ifstream and when to use old C-Style?
> Are there any advantages of using ifstream.
> As much I know ifstream in C++ is bit cleaner, more type safe. Are
> there any other advantages?
> Is ifstream usage is faster than old C-Style in C++?
>
> Please let me know the exact difference
>

If you are programming in C++ you should prefer ifstream. Using C
style file read writes is recommended over fstream only if you know
that you are dealing with a lot of binary data and you won't make much
use of fstream features and most of the code is already written that
way.

Regards,
Jyoti


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Stephen Howe on
On Mon, 15 Feb 2010 02:11:34 CST, Sadanand <steggi.cs(a)gmail.com> wrote:

>hi all,
>I am bit confused about usage of ifstream and fopen ( old C-Style),
>When exactly I should use ifstream and when to use old C-Style?
>Are there any advantages of using ifstream.

It is type safe. You can also define >> operator and manipulators for your
classes so they also work with istream's.
So it is also type-extensible.

>As much I know ifstream in C++ is bit cleaner, more type safe. Are
>there any other advantages?

Yes. ifstream's will close themselves in the destructor if still open.
Useful if an exception has been thrown.

>Is ifstream usage is faster than old C-Style in C++?

Generally not. They can be 5-10% slower. I have never seen a faster stream
implementation than stdio.
I have created wrapper objects that manage a FILE * object and will fclose() an
open file in the destructor if still open.

Stephen Howe

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: A. McKenney on
On Feb 15, 3:11 am, Sadanand <steggi...(a)gmail.com> wrote:
> hi all,
> I am bit confused about usage of ifstream and fopen ( old C-Style),
> When exactly I should use ifstream and when to use old C-Style?
> Are there any advantages of using ifstream.
> As much I know ifstream in C++ is bit cleaner, more type safe. Are
> there any other advantages?
> Is ifstream usage is faster than old C-Style in C++?

The only real rule is that you shouldn't mix them,
just as it's a bad idea to mix C stdio calls
(fopen(), fgets(), etc.) with "system" I/O
calls I/O (open(), read(), etc.)

C++ stream I/O is considered more type-safe, although
as far as I can tell, the only stdio calls that aren't
type-safe are the scanf and printf family of calls.

Which is faster is an implementation question; at
one time, C++ streams were clearly slower.

I find myself using stdio, especially the printf
family, because I need my output lined up in columns
and I find it easier to do this with printf (or,
more typically, snprintf.)

For text input, I almost invariably have to do
my own parsing, so it's a question of whether
you like getline() or fgets() better.

One advantage of stdio (or of open()) is that
you can generally get the system errno value,
which is very important in my applications.
If it's possible with fstreams, I haven't figured
it out yet.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]