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 3 5873
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.
--
OU
Remember 18th of June 2008, Democracy died that afternoon. http://frapedia.se/wiki/Information_in_English
On 9 Nov, 16:20, Obnoxious User <O...@127.0.0.1wrote:
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;
}
On Sun, 09 Nov 2008 08:42:01 -0800, Rune Allnor wrote:
On 9 Nov, 16:20, Obnoxious User <O...@127.0.0.1wrote:
>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.
[snip]
>> 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
The call to c_str() is not necessary.
ss.str("");
Like any stream if you exhaust it the stream will enter
a non functional state.
// Test stream
if(!ss.good())
std::cout<<"Stream failure"<<std::endl;
if(!(ss >source2))
std::cout<<"Stream failure"<<std::endl;
// Clears error state flags
ss.clear();
ss << source2;
ss >dest2;
std::cout << dest2.c_str() << std::endl; // Prints a blank line
The call to c_str() is not necessary.
>
return 0;
}
--
OU
Remember 18th of June 2008, Democracy died that afternoon. http://frapedia.se/wiki/Information_in_English This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Giampiero Gabbiani |
last post by:
Is it possible to reset a std::stringstream in order to reuse it once more?
I tried with flush() method without any success...
Thanks in advance
Giampiero
|
by: Dylan |
last post by:
Hi again,
In the following program I expected step 3 to assign the values 1 and
2 to iVal1 and iVal2 yet it appears ss.seekg(0, std::ios::beg) does
not move the read position back to the...
|
by: Mr Fish |
last post by:
Is it possible for me to record floats in a std::stringstream with
100% accuracy? If so how?
Here's a little prog that demonstrates how the default ss loses
accuracy
...
|
by: Marcin Kalicinski |
last post by:
Is there a vectorstream class that implements the functionality similar to
std::stringstream but with std::vector, not std::string?
cheers,
Marcin
|
by: ma740988 |
last post by:
Consider:
#include <iostream>
#include <sstream>
#include <string>
int main ( ) {
{
double pi = 3.141592653589793238;
std::stringstream a;
|
by: avidamani |
last post by:
Hi,
I have a c++ application compiled using
Compaq C++ V6.5-014 for Compaq Tru64 UNIX V5.1A (Rev. 1885)
Compiler Driver V6.5-014 (cxx) cxx Driver
The problem is that the application is...
|
by: magix |
last post by:
I got this reply in my previous post a month ago:
May I know, how can I automatically create the folder if it doesn't exist ?
In previous reply, it said:
-------------------------...
|
by: Ziyan |
last post by:
I am writing a C/C++ program that runs in background (Linux).
Therefore, normally no output would be written into standard output.
However, sometimes I want to have debug message collected and sent...
|
by: TBass |
last post by:
Hi.
I wrote a "tag" class to store values. The user gets to store most any
type he would need. Instead of getting too complicatated, I decided I
would store the value as a stringstream, then...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |