change the default separator in stream operator ">>" ?!?!?

Discussion in 'C++' started by Francesco, Sep 15, 2003.

  1. Francesco

    Francesco Guest

    Hi,

    I've a question...
    is it possible to change the default separator in the stream operator ">>"
    (for example from the blank space " " to the comma "," ) ?
    Here is an example...

    I want to read a sequence separated by blank space. I can do like
    this...(with cin, but with file is the same thing)

    2003/08/15 25 26 24.23232323
    ----------------------------------------
    std::string data;
    int val1, val2;
    float r1;

    std::cout << "Insert data: ";
    std::cin >> name >> val1 >> val2 >> r1;
    ------------------------------------------

    But if I've comma separated values like....????

    2003/08/15,25,26,24.23232323

    I know that I can use the getline where it's possible to select the
    separator, but my question is only about the ">>".
    With all values different from char or string I can di something like
    this...

    char c;
    cin >> val1 >> c >> val2 >> c >> r1;

    but with a string.... I don't know.
    Thanks for your opinions,
    Ciao, Checco.
    Francesco, Sep 15, 2003
    #1
    1. Advertising

  2. Francesco

    Jerry Coffin Guest

    In article <>, says...
    > Hi,
    >
    > I've a question...
    > is it possible to change the default separator in the stream operator ">>"
    > (for example from the blank space " " to the comma "," ) ?


    http://www.google.com/groups?selm=MPG.1949ebedb209ceaa989a43%
    40news.clspco.adelphia.net

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Sep 15, 2003
    #2
    1. Advertising

  3. Francesco

    Francesco Guest

    On Mon, 15 Sep 2003 14:43:39 GMT, Jerry Coffin <> wrote:

    > In article <>, says...
    >> Hi,
    >>
    >> I've a question...
    >> is it possible to change the default separator in the stream operator
    >> ">>" (for example from the blank space " " to the comma "," ) ?

    >
    > http://www.google.com/groups?selm=
    >


    Thanks,
    even if with my current C++ knowledge is hard for me to understand ;)

    I've thought there was something easier, I think I'll use the sstream and
    the getline to tokenize the entire record as a string.

    Thanks anyway.

    Ciao, Checco
    Francesco, Sep 15, 2003
    #3
  4. Francesco

    tom_usenet Guest

    On Mon, 15 Sep 2003 15:00:04 GMT, Francesco <> wrote:

    >On Mon, 15 Sep 2003 14:43:39 GMT, Jerry Coffin <> wrote:
    >
    >> In article <>, says...
    >>> Hi,
    >>>
    >>> I've a question...
    >>> is it possible to change the default separator in the stream operator
    >>> ">>" (for example from the blank space " " to the comma "," ) ?

    >>
    >> http://www.google.com/groups?selm=
    >>

    >
    >Thanks,
    >even if with my current C++ knowledge is hard for me to understand ;)
    >
    >I've thought there was something easier, I think I'll use the sstream and
    >the getline to tokenize the entire record as a string.


    Boost have a tokenizer class:

    http://www.boost.org/libs/tokenizer/index.htm

    Tom
    tom_usenet, Sep 15, 2003
    #4
  5. Francesco

    Francesco Guest

    On Mon, 15 Sep 2003 16:15:19 +0100, tom_usenet <>
    wrote:


    > Boost have a tokenizer class:
    >
    > http://www.boost.org/libs/tokenizer/index.htm
    >
    > Tom
    >

    Yes, I know, but I need full portability and I must use only standard
    library.
    But it's not so hard to take tokens...

    std::vector<std::string> scomponi(std::string str, char separatore)
    {
    // assegnamento della stringa al tipo stringstream std::stringstream
    dati(str); std::string buff;
    std::vector<std::string> ris;

    // divisione in tokens
    while (getline(dati, buff, separatore))
    ris.push_back(buff);
    return ris;
    }

    Thanks.
    Ciao, Checco.
    Francesco, Sep 15, 2003
    #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. John Smith
    Replies:
    2
    Views:
    414
    Ivan Vecerina
    Oct 6, 2004
  2. mrstephengross
    Replies:
    3
    Views:
    392
    James Kanze
    May 10, 2007
  3. puzzlecracker
    Replies:
    8
    Views:
    420
    James Kanze
    Apr 15, 2008
  4. Sak ..
    Replies:
    5
    Views:
    277
    Sak ..
    Feb 10, 2010
  5. sloubi
    Replies:
    2
    Views:
    212
    Dr J R Stockton
    Jul 1, 2009
Loading...

Share This Page