istringstream [英] istringstream

查看:74
本文介绍了istringstream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我发现了istringstream对象

我正在尝试读取一个字符串并将其拆分为多个字段,

由整数组成和字符串


它是用于发送消息的协议:

*消息代码,pe。 1为HELLO

*留言内容,pe。 5(种子)


两个字段都被@ +分隔开来


/ * ************ *********************************** * /

istringstream in;

in.str(" 1 @ + @ 5");


int msgcode = -1;

int seed = - 1;

string spacer;


in>> msgcode>> spacer>>种子;


cout<<"" msgcode ="<<< msgcode<<",spacer ="<< spacer<<"" s eed ="<< seed<< endl;

/ * ************************* ********************** * /


但是,当我运行它时,输出是:

msgcode = 1,spacer = @ + @ 5,seed = -1


5总是属于字符串spacer

也用空格替换@ + @或将char *作为数据类型替换为

" spacer"没有把5放在种子中。

字符串spacer"总是到达终点(如果我们设置555

而不是5),这可能是因为流不能告诉

结束的地方字符串并启动整数...


有谁知道如何绕过这个?

解决方案



k:arel写道:

/ * ****************** ***************************** * /
istringstream in;
in.str(" 1 @ + @ 5英寸);
in>> msgcode>> spacer>>种子;
字符串spacer;总是到达终点(如果我们设置555
而不是5),这可能是因为流不能告诉
结束字符串并启动整数。 。




读取一个字符串会占用任何空格。我不认为这是你问题的最佳答案。


k:arel写道:

今天我发现了istringstream对象
我正在尝试读取一个字符串并将其拆分为多个字段,
由整数和字符串组成
这是一个协议发送消息:
*消息代码,pe。 1为HELLO
*消息内容,pe。 5(种子)

两个字段都被@ +分隔开来

/ * ******************** *************************** * /
istringstream in;
in.str(" 1 @ + @ 5);

int msgcode = -1;
int seed = -1;
string spacer;

in>> msgcode>> spacer>>种子;

cout<<"" msgcode ="<<< msgcode<<",spacer ="<< spacer<<"" s eed =" ;<<种子<< endl;
/ * ********************************* ************** * /

然而,当我运行它时,输出是:
msgcode = 1,spacer = @ + @ 5, seed = -1

5总是属于字符串spacer

也用空格替换@ + @或取一个char *作为<的数据类型br />spacer没有把5放在种子中的字符串spacer中。总是到达终点(如果我们设置555
而不是5),这可能是因为流不能告诉
结束字符串并启动整数。 。




如果您使用的是VS 2005,这是库中的已知错误。


查看此主题详情:
http://groups.google.com/group/micro...d09bf204?tvc=1


ro ********** @ gmail.com 写道:

k: arel写道:

/ * ********************************* ************** * /
istringstream in;
in.str(" 1 @ + @ 5");


< >>中的blockquote class =post_quotes> msgcode>> spacer>>种子;


字符串" spacer"总是到达终点(如果我们设置555
而不是5),这可能是因为流不能告诉
结束字符串并启动整数。 。



读取一个字符串会占用任何空格。我不认为这将是你问题的最佳答案。




啊,好的,我错过了。但是,VS2005也存在问题。


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<<",spacer="<<spacer<<",s eed="<<seed<<endl;
/* *********************************************** */

yet, when i run this, the output is:
msgcode=1,spacer=@+@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?

解决方案


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<<",spacer="<<spacer<<",s eed="<<seed<<endl;
/* *********************************************** */

yet, when i run this, the output is:
msgcode=1,spacer=@+@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**********@gmail.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.


这篇关于istringstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆