M
Mark S.
Hi,
since my next exercise involves working with files, I tried to learn
some more about ifstream. So far in the book, reading from files was
generally handled like this:
ifstream in(file); // (1)
string s;
while(getline(in, s)) // (2)
...
So far, so good. But when looking around, I also found the following syntax:
ifstream in; // (3)
string s;
in.open(file); // (4)
in.getline(s, 30); // (5)
...
in.close(); // (6)
Both works, of course. The second way of handling files made more sense
to me, but I was still wondering about the syntax. Google brought up
this page:
http://www.cplusplus.com/reference/iostream/ifstream/
Now my questions:
a) On the web page, it says: "The file to be associated with the stream
can be specified either as a parameter in the constructor or by calling
member open."
Now, the way I understand it is this: (1) is opening the file via the
constructor (and creates the object "in"?) and (3) is creating the
object first, then opening the file explicitly (4) and eventually
closing it (6).
In the documentation, I don't read anything about a destructor, so I wonder:
- Why don't I need to close the file in the first example?
b) My second (more important) question has to do with getline. With (2),
the _complete_ (!) line is automatically read into the string "line"
while the second syntax seems to always require a second argument - the
length that is to be read. This is they way I also interpret the
documentation, however, the first example clearly works. So..
- I thought both example used the same functions, just with different
syntax?
- How come that the documentation does not mention how to read the
complete line? Is that part missing? Shouldn't in.getline(s) work the
same way getline(in, s) does?
The documentation says that getline() exists in fstream and in string,
could that be the problem? If so, how do I resolve it (I mean, isn't it
pretty common to have strings when you are working with files?)?
-> Basically, which way should I use to handle files? Or am I
overlooking something?
Thank you for your time!
Kind regards
Mark
since my next exercise involves working with files, I tried to learn
some more about ifstream. So far in the book, reading from files was
generally handled like this:
ifstream in(file); // (1)
string s;
while(getline(in, s)) // (2)
...
So far, so good. But when looking around, I also found the following syntax:
ifstream in; // (3)
string s;
in.open(file); // (4)
in.getline(s, 30); // (5)
...
in.close(); // (6)
Both works, of course. The second way of handling files made more sense
to me, but I was still wondering about the syntax. Google brought up
this page:
http://www.cplusplus.com/reference/iostream/ifstream/
Now my questions:
a) On the web page, it says: "The file to be associated with the stream
can be specified either as a parameter in the constructor or by calling
member open."
Now, the way I understand it is this: (1) is opening the file via the
constructor (and creates the object "in"?) and (3) is creating the
object first, then opening the file explicitly (4) and eventually
closing it (6).
In the documentation, I don't read anything about a destructor, so I wonder:
- Why don't I need to close the file in the first example?
b) My second (more important) question has to do with getline. With (2),
the _complete_ (!) line is automatically read into the string "line"
while the second syntax seems to always require a second argument - the
length that is to be read. This is they way I also interpret the
documentation, however, the first example clearly works. So..
- I thought both example used the same functions, just with different
syntax?
- How come that the documentation does not mention how to read the
complete line? Is that part missing? Shouldn't in.getline(s) work the
same way getline(in, s) does?
The documentation says that getline() exists in fstream and in string,
could that be the problem? If so, how do I resolve it (I mean, isn't it
pretty common to have strings when you are working with files?)?
-> Basically, which way should I use to handle files? Or am I
overlooking something?
Thank you for your time!
Kind regards
Mark