| re: Questions of streams
"Als" <nospam@nowhere.net> wrote in message
news:wOpKb.285059$Ec1.9843385@bgtnsc05-news.ops.worldnet.att.net...
| 1. <fstream>
| ifstream test_file ("test.txt");
| test_file >> user_name >> password;
|
| This chained reading in of the text file is not so understandable to me.
| Could someone share some insights on it?
The istream extraction operator '>>' reads white space
delimited tokens, unless you have the 'skipws' flag
set. That means, it will read one token in the first
'>>' operation, and then the next - in this case, being
the password from the file.
| 2.<sstream>
| Why is "ostringstream" needed in STL? When should use it? Has it so
| different usage than string?
Yes, the usage is different.
'stringstream(s)' are created in memory, and you can
perform operations on them in a similar fashion as you
would with files.
You could use the above in particular, to convert numeric
values to their equivalent string representations.
| 3.
| string name;
| cin >> name;
| getline (cin, name);
| cout << name << endl;
|
| Why "getline" since cin >> name there already?
| Why does the output "Doe" instead of "Joe Doe" when input is "Joe Doe"?
See above explanation for the extraction operator '>>'.
The 'getline()' function, will read a whole line of
text(including spaces) by default, but you can specify
an delimiter for it's third argument.
Cheers.
Chris Val |