R
Rune Allnor
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
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