| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"ma740988" |
| Date: |
29 Mar 2006 07:47:24 PM |
| Object: |
std::istringstream and ignore .. |
Consider the source:
# include <iostream>
# include <string>
# include <fstream>
# include <vector>
# include <sstream>
using namespace std;
int main()
{
std::fstream InOut( "MyText.txt", std::ios::binary |
std::ios::in | std::ios::out | std::ios::app );
if( !InOut )
{
std::cout << "File could not be opened\n";
return EXIT_FAILURE; // Or create it here if you want, etc...
}
const std::string dt (" ....... ");
std::string::size_type sz = dt.size();
InOut << "header" + dt + "0x0\n";
InOut << "data" + dt + "0x100\n";
InOut << "data" + dt + "0x1100\n";
InOut << "header" + dt + "0x2100\n";
InOut << "data" + dt + "0x2200\n";
InOut << "data" + dt + "0x3200\n";
InOut.seekg( std::ios_base::beg );
InOut.clear(); // is this necessary at this point?
std::string Buffer;
while ( std::getline ( InOut, Buffer ) ) // quick look
std::cout << Buffer << std::endl;
typedef std::vector<int> INT_VEC;
INT_VEC values;
std::string line;
//char delim('.');
//while( std::getline( InOut, line, delim ) ) // delimiter wont help
here
// now read file and store all 'the values corresponding to header in
a vector
while( std::getline( InOut, line ) )
{
iss.ignore( sz );
int value ( 0 );
iss >> value;
values.push_back(value);
}
// now tell the user about the values you found (should only find 2,
0 and 0x2100 )
std::cout << " ## for each header - here are your choices ## " <<
std::endl;
// display the values vector .. i.e
// For example: 1) 0
// 2) 0x2100
if ( values.size() ) // display purposes ..
{
std::copy(values.begin(), values.end(),
std::ostream_iterator<int>(std::cout, "\n"));
}
std::cin.get();
return EXIT_SUCCESS;
}
With the latter of the while loops, I search for the text 'header'.
Once found, I ignore the dots following header. Lastly I try to
extract the value from the stream object iss for storage into value.
Doesn't work and I'm not sure what I'm missing?
Thanks in advance
.
|
|
| User: "BobR" |
|
| Title: Re: std::istringstream and ignore .. |
30 Mar 2006 12:13:57 AM |
|
|
ma740988 wrote in message ...
<snip>
int main(){
<snip>
InOut.seekg( std::ios_base::beg );
InOut.clear(); // is this necessary at this point?
If 'seekg()' failed, then yes!
typedef std::vector<int> INT_VEC;
INT_VEC values;
Why? Why not just:
std::vector<int> values;
std::string line;
while( std::getline( InOut, line ) ){
iss.ignore( sz );
What is 'iss'?
int value ( 0 );
iss >> value;
values.push_back(value);
}
std::cout << " ## for each header - here are your choices ## " <<
std::endl;
if ( values.size() ) // display purposes ..
{
std::copy(values.begin(), values.end(),
std::ostream_iterator<int>(std::cout, "\n"));
}
std::cin.get();
return EXIT_SUCCESS;
}
With the latter of the while loops, I search for the text 'header'.
Once found, I ignore the dots following header. Lastly I try to
extract the value from the stream object iss for storage into value.
Doesn't work and I'm not sure what I'm missing?
You are missing 'iss'!
"Doesn't work" doesn't work! Need more INFORMATION!!
--
Bob R
POVrookie
.
|
|
|
| User: "ma740988" |
|
| Title: Re: std::istringstream and ignore .. |
30 Mar 2006 05:44:08 AM |
|
|
What is 'iss'?
Bob, my aplogies. I'm not sure where I went wrong with my post, might
have changed something while posting. In any event here goes:
while( std::getline( InOut, line ) )
{
std::istringstream iss( line );
if ( iss == "header" )
{
iss.ignore( sz );
int value ( 0 );
iss >> value;
values.push_back(value);
}
}
[...]
--
Bob R
POVrookie
.
|
|
|
| User: "BobR" |
|
| Title: Re: std::istringstream and ignore .. |
30 Mar 2006 05:50:24 PM |
|
|
ma740988 wrote in message
<1143719048.640161.219740@z34g2000cwc.googlegroups.com>...
What is 'iss'?
Bob, my aplogies. I'm not sure where I went wrong with my post, might
have changed something while posting. In any event here goes:
while( std::getline( InOut, line ) ){
std::istringstream iss( line );
if( iss == "header" ){
// error: ambiguous overload for 'operator==' in 'iss == "header"'
iss.ignore( sz );
int value ( 0 );
iss >> value;
values.push_back(value);
}
}
[...]
Did that compile for you?
How did you overload 'operator==' for 'stringstream'?
I think what you want is more like:
// InOut << "header" + dt + "0x2100\n";
std::string line("header ....... 0x2100\n"); // simulated input
if( line.find("header") != line.npos ){
int value( 0 );
std::cout<<" line="<<line<<std::endl;
for(size_t i(0); i < line.size(); ++i){
if( std::isdigit( line.at( i ) ) ){
std::cout<<" line.substr(i)="
<<line.substr( i )<<std::endl;
std::istringstream sStream( line.substr( i ) );
sStream >>std::hex>> value;
// values.push_back(value);
break;
} // if(isdigit)
} // for(i)
std::cout<<" header found. value="<<value<<std::endl;
} // if(find)
// --- output ---
// line=header ....... 0x2100
// line.substr(i)=0x2100
// header found. value=8448
Use 'std::string' for what it's good for and 'std::stringstream' for what
it's good for.
Don't try to make one do the others job.
KISS, refactor later (if needed)! <G>
--
Bob R
POVrookie
.
|
|
|
| User: "ma740988" |
|
| Title: Re: std::istringstream and ignore .. |
30 Mar 2006 07:38:06 PM |
|
|
BobR wrote:
ma740988 wrote in message
<1143719048.640161.219740@z34g2000cwc.googlegroups.com>...
What is 'iss'?
Bob, my aplogies. I'm not sure where I went wrong with my post, might
have changed something while posting. In any event here goes:
while( std::getline( InOut, line ) ){
std::istringstream iss( line );
if( iss == "header" ){
// error: ambiguous overload for 'operator==' in 'iss == "header"'
iss.ignore( sz );
int value ( 0 );
iss >> value;
values.push_back(value);
}
}
[...]
Did that compile for you?
Interesting.
c:\development\file_io_test\file_io_test.cpp(378) : warning C4267:
'argument' : conversion from 'size_t' to 'std::streamsize', possible
loss of data
Build log was saved at
"file://c:\development\file_io_test\Debug\BuildLog.htm"
file_io_test - 0 error(s), 7 warning(s)
---------------------- Done ----------------------
Build: 1 succeeded, 0 failed, 0 skipped
The warning points to iss.ignore ( sz );
What compiler are you using? I'm using MSVC.NET
How did you overload 'operator==' for 'stringstream'?
I think what you want is more like:
// InOut << "header" + dt + "0x2100\n";
std::string line("header ....... 0x2100\n"); // simulated input
if( line.find("header") != line.npos ){
int value( 0 );
std::cout<<" line="<<line<<std::endl;
for(size_t i(0); i < line.size(); ++i){
if( std::isdigit( line.at( i ) ) ){
std::cout<<" line.substr(i)="
<<line.substr( i )<<std::endl;
std::istringstream sStream( line.substr( i ) );
sStream >>std::hex>> value;
// values.push_back(value);
break;
} // if(isdigit)
} // for(i)
std::cout<<" header found. value="<<value<<std::endl;
} // if(find)
// --- output ---
// line=header ....... 0x2100
// line.substr(i)=0x2100
// header found. value=8448
Use 'std::string' for what it's good for and 'std::stringstream' for what
it's good for.
Don't try to make one do the others job.
KISS, refactor later (if needed)! <G>
Indeed... That's what I wanted .. Thanks alot
.
|
|
|
| User: "BobR" |
|
| Title: Re: std::istringstream and ignore .. |
31 Mar 2006 03:25:34 PM |
|
|
ma740988 wrote in message ...
BobR wrote:
std::istringstream iss( line );
if( iss == "header" ){
// error: ambiguous overload for 'operator==' in 'iss == "header"'
[...]
}
Did that compile for you?
Interesting.
c:\development\file_io_test\file_io_test.cpp(378) : warning C4267:
'argument' : conversion from 'size_t' to 'std::streamsize', possible
loss of data
Build log was saved at
"file://c:\development\file_io_test\Debug\BuildLog.htm"
file_io_test - 0 error(s), 7 warning(s)
---------------------- Done ----------------------
Build: 1 succeeded, 0 failed, 0 skipped
The warning points to iss.ignore ( sz );
What compiler are you using? I'm using MSVC.NET
GCC 3.3.1 MinGW (g++).
Maybe one of the 'pros' will drop by and comment on which compiler is more
correct.
--
Bob R
POVrookie
.
|
|
|
|
|
|
|
|

|
Related Articles |
|
|