Questions of streams

Discussion in 'C++' started by Als, Jan 6, 2004.

  1. Als

    Als Guest

    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.
     
    Als, Jan 6, 2004
    #1
    1. Advertising

  2. "Als" <> wrote in message
    news:wOpKb.285059$...
    | 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
     
    Chris \( Val \), Jan 6, 2004
    #2
    1. Advertising

  3. "Chris \( Val \)" <> 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.
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
     
    Dietmar Kuehl, Jan 6, 2004
    #3
  4. "Dietmar Kuehl" <> wrote in message
    news:...
    | "Chris \( Val \)" <> 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
     
    Chris \( Val \), Jan 6, 2004
    #4
  5. Als

    Default User Guest

    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
     
    Default User, Jan 6, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pawel Banys
    Replies:
    1
    Views:
    494
    Joe Smith
    Jul 8, 2004
  2. Justin Weinberg

    Audio Streams from ASP.NET

    Justin Weinberg, Oct 14, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    355
    Justin Weinberg
    Oct 14, 2003
  3. Detlef Hüttenbach

    **Bug**: Handling Streams from within ashx handlers

    Detlef Hüttenbach, Jan 25, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    427
    Hermit Dave
    Jan 25, 2004
  4. Mantorok Redgormor

    couple of questions on streams

    Mantorok Redgormor, Oct 27, 2004, in forum: C Programming
    Replies:
    9
    Views:
    359
    Malcolm
    Oct 30, 2004
  5. Replies:
    28
    Views:
    796
    John W. Kennedy
    Jan 9, 2008
Loading...

Share This Page