473,466 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Mar 30 '06 #1
5 8665

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
Mar 30 '06 #2
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


Mar 30 '06 #3

ma740988 wrote in message
<11**********************@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
Mar 31 '06 #4

BobR wrote:
ma740988 wrote in message
<11**********************@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

Mar 31 '06 #5

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(37 8) : 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
Mar 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Samuele Armondi | last post by:
Hi everyone, Since istringstream objects are not assignable, I'm using the following code to allocate some dynamically. My question is: Is this the correct way of doing it? Am I deleting all the...
6
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
7
by: Luther Baker | last post by:
Hi, My question is regarding std::istringstream. I am serializing data to an ostringstream and the resulting buffer turns out just fine. But, when I try the reverse, when the istringstream...
6
by: JustSomeGuy | last post by:
I am passing an istringstream to a function. I want that function to get a copy of the istringstream and not a refrence to it. ie when the function returns I want the istringstream to be...
1
by: Adam Parkin | last post by:
Hello all, I'm trying to write a function which given a std::string parses the string by breaking the sentance up by whitespace (\t, ' ', \n) and returns the result as a vector of strings. Here's...
10
by: k:arel | last post by:
today i've discovered the istringstream object i'm trying to read a string and split it up in a number of fields, consisting of integers and strings it's for a protocol that sends messages with:...
7
by: mathieu | last post by:
Hello, Is there a way to construct a std::istringstream from a buffer of char and avoid copying the array of bytes ? const char s = "Hello, World"; std::string str(s); std::istringstream is;...
4
by: Dave Townsend | last post by:
Hi, I have to read some memory data from a stream. This would be in the following format, for example: 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 that is i have 8 values on a line, separated...
12
by: sergey.lukoshkin | last post by:
Hello everyone! My task is in converting numbers from string to int variables. I used istringstream to perform it. So I wrote simple test function. But it doesn't work as I expected because...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.