Questions of streams

A

Als

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.
 
C

Chris \( Val \)

| 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
 
D

Dietmar Kuehl

Chris \( Val \) said:
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.
 
C

Chris \( Val \)

| > 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
 
D

Default User

Als wrote:

[ a bunch of obvious beginner questions ]
Any comment is appreciated.


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




Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top