Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Questions of streams

Reply
Thread Tools

Questions of streams

 
 
Als
Guest
Posts: n/a
 
      01-06-2004
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.


 
Reply With Quote
 
 
 
 
Chris \( Val \)
Guest
Posts: n/a
 
      01-06-2004

"Als" <(E-Mail Removed)> wrote in message
news:wOpKb.285059$(E-Mail Removed)...
| 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








 
Reply With Quote
 
 
 
 
Dietmar Kuehl
Guest
Posts: n/a
 
      01-06-2004
"Chris \( Val \)" <(E-Mail Removed)> 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.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
 
Reply With Quote
 
Chris \( Val \)
Guest
Posts: n/a
 
      01-06-2004

"Dietmar Kuehl" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
| "Chris \( Val \)" <(E-Mail Removed)> 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


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      01-06-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Questions about buffered streams failure_to@yahoo.co.uk Java 28 01-09-2008 07:47 PM
couple of questions on streams Mantorok Redgormor C Programming 9 10-30-2004 09:17 AM
how to use open with many input streams and capture output? Pawel Banys Perl 1 07-08-2004 06:32 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM
Audio Streams from ASP.NET Justin Weinberg ASP .Net 0 10-14-2003 05:59 PM



Advertisments