Trouble using std::stringstream

Discussion in 'C++' started by Rune Allnor, Nov 9, 2008.

  1. Rune Allnor

    Rune Allnor Guest

    Hi all.

    I am trying to use std::stringstream to validate input from a file.
    The strategy is to read a line from the file into a std::string
    and feed this std::string to an object which breaks it up into
    individual elements. The elements can be strings, integers
    or floating point numbers.

    In the object where I break the line into elements I use a
    std::stringstream object to do the actual check:

    std::stringstream ss_;
    std::string destination1;
    std::string destination2;

    ss_ << source.substr(i,j-i);
    ss_ >> destination1;
    // destination1 contains the desired string
    ss_.str(""); // Prep for the next item

    // update i and j

    // Preferred syntax
    ss_ << source.substr(i,j-i);
    ss_ >> destination2;
    // destination2 is empty

    This works, but only partially: The destination1 string
    contains the desired result, but the destination2
    string always comes up empty.

    The alternative implementation,

    std::string tmp = source.substr(i,j-i);
    // Tmp now contains the desire result
    ss_ << tmp;
    ss_ >> destination2;

    shows that I am able to extract the desired substring,
    but for some reason the std::stringstream object doesn't
    work.

    Any ideas what might be wrong?
    Any suggestions about other ways of reading/validating
    file I/O? Regular expressions is one option, but it seems
    to be a nightmare to cover all those different number
    formats for floating point...

    Rune
     
    Rune Allnor, Nov 9, 2008
    #1
    1. Advertising

  2. Rune Allnor

    Rune Allnor Guest

    On 9 Nov, 16:20, Obnoxious User <O...@127.0.0.1> wrote:
    > On Sun, 09 Nov 2008 06:02:50 -0800, Rune Allnor wrote:
    > > Hi all.

    >
    > > I am trying to use std::stringstream to validate input from a file. The
    > > strategy is to read a line from the file into a std::string and feed
    > > this std::string to an object which breaks it up into individual
    > > elements. The elements can be strings, integers or floating point
    > > numbers.

    >
    > > In the object where I break the line into elements I use a
    > > std::stringstream object to do the actual check:

    >
    > > std::stringstream ss_;
    > > std::string destination1;
    > > std::string destination2;

    >
    > > ss_ << source.substr(i,j-i);
    > > ss_ >> destination1;
    > > // destination1 contains the desired string ss_.str("");      // Prep
    > > for the next item

    >
    > > // update i and j

    >
    > > // Preferred syntax
    > > ss_ << source.substr(i,j-i);
    > > ss_ >> destination2;
    > > // destination2 is empty

    >
    > > This works, but only partially: The destination1 string contains the
    > > desired result, but  the destination2 string always comes up empty.

    >
    > > The alternative implementation,

    >
    > > std::string tmp = source.substr(i,j-i);
    > >     // Tmp now contains the desire result
    > > ss_ << tmp;
    > > ss_ >> destination2;

    >
    > > shows that I am able to extract the desired substring, but for some
    > > reason the std::stringstream object doesn't work.

    >
    > > Any ideas what might be wrong?
    > > Any suggestions about other ways of reading/validating file I/O? Regular
    > > expressions is one option, but it seems to be a nightmare to cover all
    > > those different number formats for floating point...

    >
    > Post a minimal compilable program that demonstrates the
    > error you perceive.


    Code appended below. Of course, I don't validate strings
    in this way - I had in mind to use this technique to
    validate numbers - but it would help to know what is
    going on.

    And even if I choose regular expressions to validate
    the text, it seems I need some similar trick to convert
    numbers from text to binary, similar to C's scanf().

    Rune

    //#include "stdafx.h" // Compiled with VS2005
    #include <sstream>
    #include <iostream>
    #include <string>

    int main(int argc, char* argv[])
    {
    std::string source1("Text1");
    std::string source2("Text2");
    std::string dest1;
    std::string dest2;
    std::stringstream ss;

    ss << source1;
    ss >> dest1;
    std::cout << dest1.c_str() << std::endl; // Works as expected
    ss.str("");

    ss << source2;
    ss >> dest2;
    std::cout << dest2.c_str() << std::endl; // Prints a blank line

    return 0;
    }
     
    Rune Allnor, Nov 9, 2008
    #2
    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. Giampiero Gabbiani

    std:stringstream reset

    Giampiero Gabbiani, Oct 19, 2003, in forum: C++
    Replies:
    6
    Views:
    25,394
    Giampiero Gabbiani
    Oct 19, 2003
  2. KidLogik
    Replies:
    2
    Views:
    7,902
    Alberto Barbati
    Feb 24, 2004
  3. Christopher Benson-Manica
    Replies:
    5
    Views:
    6,477
    Michiel Salters
    May 3, 2004
  4. Vijai Kalyan
    Replies:
    4
    Views:
    6,041
  5. Jeffrey Walton
    Replies:
    10
    Views:
    966
    Mathias Gaunard
    Nov 26, 2006
Loading...

Share This Page