Connecting Tech Pros Worldwide Forums | Help | Site Map

Questions of streams

Als
Guest
 
Posts: n/a
#1: Jul 22 '05
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?

2.<sstream>
Why is "ostringstream" needed in STL? When should use it? Has it so
different usage than string?

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"?

Any comment is appreciated.


Chris \( Val \)
Guest
 
Posts: n/a
#2: Jul 22 '05

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








Dietmar Kuehl
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Questions of streams


"Chris \( Val \)" <chrisval@bigpond.com.au> wrote:[color=blue]
> The istream extraction operator '>>' reads white space
> delimited tokens, unless you have the 'skipws' flag
> set.[/color]

Well, actually the extraction operator for 'std::string' always reads white
space delimited tokens. The difference between 'skipws' being set or not is
whether it will skip leading white space before attempting to read a token.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Chris \( Val \)
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Questions of streams



"Dietmar Kuehl" <dietmar_kuehl@yahoo.com> wrote in message
news:5b15f8fd.0401060107.6f73bd06@posting.google.c om...
| "Chris \( Val \)" <chrisval@bigpond.com.au> wrote:
| > The istream extraction operator '>>' reads white space
| > delimited tokens, unless you have the 'skipws' flag
| > set.
|
| Well, actually the extraction operator for 'std::string' always reads white
| space delimited tokens. The difference between 'skipws' being set or not is
| whether it will skip leading white space before attempting to read a token.

Oh yes, you are correct - my apologies.

Cheers.
Chris Val


Default User
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Questions of streams


Als wrote:

[ a bunch of obvious beginner questions ]
[color=blue]
> Any comment is appreciated.[/color]


What text are you using that doesn't cover this material? Why are you
unable to look up things like >> and getline()?




Brian Rodenborn
Closed Thread