std::istringstream and ignore ..

M

ma740988

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::eek:ut | 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::eek:stream_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
 
B

BobR

ma740988 wrote in message ...
int main(){
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 said:
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::eek:stream_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!!
 
M

ma740988

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);
}
}

[...]

 
B

BobR

ma740988 wrote in message
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>
 
M

ma740988

BobR said:
ma740988 wrote in message
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
 
B

BobR

ma740988 wrote in message ...
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top