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:
* message code, pe. 1 for HELLO
* message content, pe. 5 (seed)
both field are separated by @+@
/* *************** *************** *************** ** */
istringstream in;
in.str("1@+@5") ;
int msgcode=-1;
int seed=-1;
string spacer;
in >> msgcode >> spacer >> seed;
cout<<"msgcode= "<<msgcode<<",s pacer="<<spacer <<",seed="<<see d<<endl;
/* *************** *************** *************** ** */
yet, when i run this, the output is:
msgcode=1,space r=@+@5,seed=-1
the 5 always belongs to the string "spacer"
also replacing the @+@ by a space or taking a char* as data type for
"spacer" doesn't put the 5 in the "seed"
the string "spacer" alway reaches to the end (also if we set there 555
instead of just 5), it's probably because the stream can't tell where
to end the string and start the integer...
has anybody any idea how i could bypass this? 10 6016
k:arel wrote: /* *************** *************** *************** ** */ istringstream in; in.str("1@+@5") ;
in >> msgcode >> spacer >> seed;
the string "spacer" alway reaches to the end (also if we set there 555 instead of just 5), it's probably because the stream can't tell where to end the string and start the integer...
Reading a string gobbles up everything up to any whitespace. I don't
think it will be the best answer to your problem.
k:arel wrote: 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: * message code, pe. 1 for HELLO * message content, pe. 5 (seed)
both field are separated by @+@
/* *************** *************** *************** ** */ istringstream in; in.str("1@+@5") ;
int msgcode=-1; int seed=-1; string spacer;
in >> msgcode >> spacer >> seed;
cout<<"msgcode= "<<msgcode<<",s pacer="<<spacer <<",seed="<<see d<<endl; /* *************** *************** *************** ** */
yet, when i run this, the output is: msgcode=1,space r=@+@5,seed=-1
the 5 always belongs to the string "spacer"
also replacing the @+@ by a space or taking a char* as data type for "spacer" doesn't put the 5 in the "seed" the string "spacer" alway reaches to the end (also if we set there 555 instead of just 5), it's probably because the stream can't tell where to end the string and start the integer...
If you're using VS 2005, this is a known bug in the library.
See this thread for details: http://groups.google.com/group/micro...d09bf204?tvc=1 ro**********@gm ail.com wrote: k:arel wrote:
/* *************** *************** *************** ** */ istringstream in; in.str("1@+@5") ;
in >> msgcode >> spacer >> seed;
the string "spacer" alway reaches to the end (also if we set there 555 instead of just 5), it's probably because the stream can't tell where to end the string and start the integer...
Reading a string gobbles up everything up to any whitespace. I don't think it will be the best answer to your problem.
Ah, good point, I missed that. However, there is also an issue with VS2005.
red floyd wrote: k:arel wrote: 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: * message code, pe. 1 for HELLO * message content, pe. 5 (seed)
both field are separated by @+@
/* *************** *************** *************** ** */ istringstream in; in.str("1@+@5") ;
int msgcode=-1; int seed=-1; string spacer;
in >> msgcode >> spacer >> seed;
cout<<"msgcode= "<<msgcode<<",s pacer="<<spacer <<",seed="<<see d<<endl; /* *************** *************** *************** ** */
yet, when i run this, the output is: msgcode=1,space r=@+@5,seed=-1
the 5 always belongs to the string "spacer"
also replacing the @+@ by a space or taking a char* as data type for "spacer" doesn't put the 5 in the "seed" the string "spacer" alway reaches to the end (also if we set there 555 instead of just 5), it's probably because the stream can't tell where to end the string and start the integer...
If you're using VS 2005, this is a known bug in the library.
See this thread for details: http://groups.google.com/group/micro...d09bf204?tvc=1
Ignore this. Noah had the correct answer. ro**********@gm ail.com wrote: k:arel wrote:
/* *************** *************** *************** ** */ istringstream in; in.str("1@+@5") ;
in >> msgcode >> spacer >> seed;
the string "spacer" alway reaches to the end (also if we set there 555 instead of just 5), it's probably because the stream can't tell where to end the string and start the integer...
Reading a string gobbles up everything up to any whitespace. I don't think it will be the best answer to your problem.
Noah is right reading string messes you up. Addition of "@+@", is it
required by the protocol or is it something that you have added as a
seperator. If protocol does not require that then you add a seperator
like "+ " (replace @ with space on ight hand side of +). By this when
reading string the stream will stop after getting a space.
-- Nitin Motgi. ro**********@gm ail.com wrote in message ... k:arel wrote: /* *************** *************** *************** ** */ istringstream in; in.str("1@+@5") ; in >> msgcode >> spacer >> seed;
the string "spacer" alway reaches to the end (also if we set there 555 instead of just 5), it's probably because the stream can't tell where to end the string and start the integer...
Reading a string gobbles up everything up to any whitespace. I don't think it will be the best answer to your problem.
k:arel,
Try 'getline()' with a delimiter:
{
std::istringstr eam isin("25 This is a string\n 09 1876");
int num1(0), num2(0);
std::string thestring;
isin >> num1;
isin.ignore(1); // skip the space
std::getline( isin, thestring, '\n' );
isin >> num2; // leading space ignored
std::cout <<"num1="<<num1 <<" thestring="<<th estring
<<" num2="<<num2<<s td::endl;
// out: num1=25 thestring=This is a string num2=9
}
That help you k:arel?
Thanks for the use of your post, Mr. roberts. <G>
--
Bob R
POVrookie
ok, this did the trick:
i'm free to choose the seperator, but since where sending bash
commands, the seperator should be "weird".
i replaced the spaces bij underscores, so it's clearer:
this works: in.str("1+_5");
this works: in.str("1+_+5") ;
this works: in.str("1@+@_5" );
! mention the spaces !
@red floyd, i'm using KDevelop3 for programming C++ because i'm using
Unix resources in my program.
i also found this solution:
/* *************** *************** *************** ** */
istringstream in;
in.str("1@+@55" );
int msgcode=-1;
int seed=-1;
char ch;
in >> msgcode >> ch>>ch>>ch>> seed;
/* *************** *************** *************** ** */
yet this solution is NOT user friendly: we won't be able to change the
separator in a normal way then...
though you could (!) solve it like this:
/* *************** *************** *************** ** */
istringstream in;
in.str("1@+@55" );
int msgcode=-1;
int seed=-1;
in >> msgcode;
readSeparator(i n, separator);
in >> seed;
void readSeparator(i stringstream &in, string separator) {
char ch;
for(int i=0; i<separator.siz e() ; i++) {
in >> ch;
}
}
/* *************** *************** *************** ** */
thanks for the replies!
Bob,
your method works fine if the separator is only one character.
My separator is 3 characters: @+@.
As i said before, i'm free to choose any separator, but i must be able
to separate the fields in an obvious way.
I could replace my separator @+@ with an other character, not used.
But this character has to be chosen at first and it's difficult to say
which character is NOT going to be used in any of the commands.
Of the separator @+@, you could say, with a certain certitude, that it
will never be send...
k:arel wrote: Bob, your method works fine if the separator is only one character.
My separator is 3 characters: @+@. As i said before, i'm free to choose any separator, but i must be able to separate the fields in an obvious way. I could replace my separator @+@ with an other character, not used. But this character has to be chosen at first and it's difficult to say which character is NOT going to be used in any of the commands. Of the separator @+@, you could say, with a certain certitude, that it will never be send...
Well then do it properly. Pick an escape character.
Take for example, C strings if you want to have a string containing a
'\' you type it twice "\\". Now, if you see a single instance of that
character, the next character is special. If you see it twice, you want
the original character.
Pick a separator that is, say '@'.
So if your original message contains 2 fields, that are delimiter
separated and the fields are "field1", "fi@ld2", then the encoded
message is "field1@fi@@ld2 ".
You'll have to use '@' as the delimiter and then if the next field
starts with a @, you have to concatenate it to the current field.
Ben Pope
--
I'm not just a number. To many, I'm known as a string... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 allocated memory correctly? Or am I missing
something glaringly simple?
Thanks in advance,
S. Armondi
std::istringstream** ArgStream;
|
by: Agent Mulder |
last post by:
I try to remove the spaces from a string
using an old trick that involves an
istringstream object. I expect the while-condition
while(istringstream>>string) to evaluate to
false once the istringstream is exhausted, but
that is not the case. What am I missing?
#include<iostream>
#include<sstream>
#include<string>
|
by: bml |
last post by:
Could you help and answer my questions of istringstream? Thanks a lot!
1. Reuse an "istringstream"
istringstream ist;
ist.str("This is FIRST test string");
ist.str("This is SECOND test string");
cout << ist.str() << endl;
|
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 encounters the two
byte shorts, it either thinks it has reached the null terminator? or
eof and consequently stops reading values back in. It doesn't matter
whether or not I use the std::ios::binary flag when opening the
istringstream or the...
|
by: Brandon |
last post by:
I'm using an istringstream to convert some integers stored in string
form to integers. However, each integer is stored in a differnt
string. So, I used istringstream's str(string) method for the first
string and then used the >> operator to extract the integer. It
worked just fine. So, on to the second string. I used str(string)
again but this time extraction gave me a seemingly random number
(-858993460). Once an istringstream is...
| |
by: dinks |
last post by:
Hi I'm really new to c++ so please forgive me if this is really basic
but im stuck... I am trying to make a data class that uses
istringstram and overloaded << and >> operators to input and output
data. The data comes in string lines like "OREBlegQ 14854 731.818"
which need to be split into a string, int and double when stored in
the class. Can anyone help? This is what i have so far:
/* Begin Code */
#include <sstream>
|
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
unmodified...
However when I try to pass it
fn(istringstream s) // doesn't compile
but
fn(istringstream & s) // does.
|
by: Randy Yates |
last post by:
Why does this:
string AWord(string& line)
{
return line;
}
bool MYOBJECT::MyFunction(string &line)
{
int day;
|
by: James Aguilar |
last post by:
Hello all,
I am trying to use an istringstream to do some input off of cin by lines.
The following snippet does not work:
char buf;
cin.getline(buf, 90);
istringstream line1(string(buf));
|
by: icanoop |
last post by:
I would like to do this
MyClass x;
istringstream("XXX") >> x; // Works in VC++ but not GCC
instead of
MyClass x;
istringstream iss("XXX");
iss >> x; // Works in both GCC and VC++
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 we have to send another system
| |