Topic: File input stream


Author: kanze@gabi-soft.de
Date: 2000/03/01
Raw View
Xiao Ma <xma@cs.uoregon.edu> writes:

|>  Hi,
|>
|>  Can someone explain this to me:
|>  Why does an ifstream variable need to be initialized on declaration? I.e,
|>  the following code piece gives an error when I try to read an integer,

Because there's an error in your compiler or your library.  Neither of
your examples should even compile.

|>   int n;
|>   ifstream fin;
|>
|>   fin = ifstream("MyInput");

Illegal: ifstream does not support assignment.

|>   fin >> n;
|>
|>  but the following does not.
|>
|>   int n;
|>   ifstream fin = ifstream("MyInput");

Illegal: ifstream does not support copy construction.

    ifstream fin( "MyInput" ) ;

is legal, but personally, I prefer to separate the open from the
construction -- it's easier to test the return code.

|>   fin >> n;

--
James Kanze                               mailto:kanze@gabi-soft.de
Conseils en informatique orient   e objet/
                   Beratung in objektorientierter Datenverarbeitung
Ziegelh   ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Xiao Ma <xma@cs.uoregon.edu>
Date: 2000/02/28
Raw View
Hi,

Can someone explain this to me:
Why does an ifstream variable need to be initialized on declaration? I.e,
the following code piece gives an error when I try to read an integer,

 int n;
 ifstream fin;

 fin = ifstream("MyInput");
 fin >> n;

but the following does not.

 int n;
 ifstream fin = ifstream("MyInput");

 fin >> n;

Thanks!

UUU  UUU
 U    U
 U   OUOO
 U  O U  O
  UUOU   O
    O    O
     OOOO

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Matt Graham <mattyg@chicagomayday.org>
Date: 2000/02/28
Raw View
Hello,

You should get a sytax error when you try it like that.  Try using the open
method of the new ifstream object you've created like this:

int n;
ifstream fin;

fin.open("MyInput");
fin >> n;

Hope that helps!

Matt



Xiao Ma wrote:

> Hi,
>
> Can someone explain this to me:
> Why does an ifstream variable need to be initialized on declaration? I.e,
> the following code piece gives an error when I try to read an integer,
>
>         int n;
>         ifstream fin;
>
>         fin = ifstream("MyInput");
>         fin >> n;
>
> but the following does not.
>
>         int n;
>         ifstream fin = ifstream("MyInput");
>
>         fin >> n;
>
> Thanks!
>
> UUU  UUU
>  U    U
>  U   OUOO
>  U  O U  O
>   UUOU   O
>     O    O
>      OOOO
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Xavier Glattard" <xavier.glattard@netcourrier.com>
Date: 2000/02/29
Raw View
Xiao Ma <xma@cs.uoregon.edu> a    crit dans le message :
Pine.GSO.4.20.0002271004040.6978-100000@ix.cs.uoregon.edu...
> Hi,
>
> Can someone explain this to me:
> Why does an ifstream variable need to be initialized on declaration? I.e,
> the following code piece gives an error when I try to read an integer,
>
> int n;
> ifstream fin;
>
> fin = ifstream("MyInput");
> fin >> n;
>
> but the following does not.
>
> int n;
> ifstream fin = ifstream("MyInput");
>
> fin >> n;

Hi !

I read somewhere that assigning stream is illegal.
The assignment operator is private.
The second isn't assignement : it's an initialization (with the copy
constructor) and is legal.
( ' ifstream fin("MyInput"); ' is simpler )

You may assign streambuf.

Xavier Glattard
-- Visiovox Telecom


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Paul Lutus" <nospam@nosite.com>
Date: 2000/02/29
Raw View
IMHO, they're both bad form in any case.

Do it this way:

ifsteam fin("filename");

This may seem like splitting hairs, but don't decide until you have more
experience.

--

Paul Lutus
www.arachnoid.com


Xiao Ma <xma@cs.uoregon.edu> wrote in message
news:Pine.GSO.4.20.0002271004040.6978-100000@ix.cs.uoregon.edu...
> Hi,
>
> Can someone explain this to me:
> Why does an ifstream variable need to be initialized on declaration? I.e,
> the following code piece gives an error when I try to read an integer,
>
> int n;
> ifstream fin;
>
> fin = ifstream("MyInput");
> fin >> n;
>
> but the following does not.
>
> int n;
> ifstream fin = ifstream("MyInput");
>
> fin >> n;
>
> Thanks!
>
> UUU  UUU
>  U    U
>  U   OUOO
>  U  O U  O
>   UUOU   O
>     O    O
>      OOOO
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]