fstream operator>> stops at white space

Discussion in 'C++' started by khalid302@gmail.com, Aug 8, 2008.

  1. Guest

    I need to read a specific number of characters into an std::string
    from a file regardless of the characters read.

    * fstream operator>> stops when it runs into a white space character.
    * The global variant of fstream::getline works with std:string but
    does not take a 'width' parameter.
    * ifstream::get takes a 'width' parameter but does not work with
    std::string.

    I simply want to read a specific number of characters and only stop on
    EOF. Is there any straight forward way to do it ?
    , Aug 8, 2008
    #1
    1. Advertising

  2. Guest

    > Resize the string first, and rely on the fact that in all (known) current
    > implementations it has contiguous buffer, and that will be guaranteed in C++0x.


    I think std:string automatically accommodates input from operator>>.
    am I missing something ?

    > Cheers, & hth.,
    >
    > - Alf
    , Aug 8, 2008
    #2
    1. Advertising

  3. Ian Collins Guest

    wrote:
    >> Resize the string first, and rely on the fact that in all (known) current
    >> implementations it has contiguous buffer, and that will be guaranteed in C++0x.

    >
    > I think std:string automatically accommodates input from operator>>.
    > am I missing something ?
    >

    I think Alf was telling you to do an unformatted read directly into the
    string's buffer.

    --
    Ian Collins.
    Ian Collins, Aug 8, 2008
    #3
  4. Guest

    On Aug 9, 1:44 am, Ian Collins <> wrote:

    > I think Alf was telling you to do an unformatted read directly into the
    > string's buffer.


    What's the API for that, std::string has data() and c_str(), and both
    return a const char* which can't/shouldn't be modified.
    , Aug 9, 2008
    #4
  5. Guest

    The only solution I could think of, is to create a custom stream class
    over cstdio ... C streams seem to be more mature.
    , Aug 9, 2008
    #5
  6. Ian Collins Guest

    wrote:
    > On Aug 9, 1:44 am, Ian Collins <> wrote:
    >
    >> I think Alf was telling you to do an unformatted read directly into the
    >> string's buffer.

    >
    > What's the API for that, std::string has data() and c_str(), and both
    > return a const char* which can't/shouldn't be modified.


    &string[0]

    --
    Ian Collins.
    Ian Collins, Aug 9, 2008
    #6
  7. wrote:
    > The only solution I could think of, is to create a custom stream class
    > over cstdio ... C streams seem to be more mature.


    std::string buffer(width, ' ');
    inputFile.read(&buffer[0], width);

    What's so hard about that?
    Juha Nieminen, Aug 9, 2008
    #7
  8. Guest

    On Aug 9, 11:48 am, Juha Nieminen <> wrote:

    >   What's so hard about that?


    Nothing, just never crossed my mind, thanks :)
    , Aug 9, 2008
    #8
  9. Guest

    On Aug 9, 11:03 am, Ian Collins <> wrote:

    > &string[0]


    Yeah, thanks.
    , Aug 9, 2008
    #9
    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. Armando
    Replies:
    6
    Views:
    745
    Armando
    Jan 29, 2004
  2. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,884
    Bryan Bullard
    Jul 11, 2003
  3. Christian Seberino
    Replies:
    21
    Views:
    1,649
    Stephen Horne
    Oct 27, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    1,006
    Steve Lamb
    Oct 23, 2003
  5. Ben C
    Replies:
    6
    Views:
    2,151
    Leif K-Brooks
    Jan 28, 2007
Loading...

Share This Page