将字符串输入ostringstream只使用第一个null? [英] Feeding string into ostringstream only uses up to the first null?

查看:88
本文介绍了将字符串输入ostringstream只使用第一个null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个用于存储原始二进制数据的std :: strings,

每个都可能包含空字节或

点。我想把它们一起打成一个字符串,所以我试过了

a std :: ostringstream:


std :: ostringstream oss;

oss<< x<< y<< z;

std :: string result(oss.str());


结果显示用字符串输入ostringstream只是

将字符串的数据带到(并且不包括)第一个空的

字符。


Stroustrop的书很模糊关于应该发生什么(至少在我能找到的唯一参考资料中);它只是说<<<<<<<运算符将一个

字符串写入ostream。


显然我可以使用+运算符连接字符串。

但是我在想 - 是否还有其他类型的流?

应该用于我想要的目的?或者一些流

操纵者?


我也想知道观察到的行为是否正确在

第一名。就像我说的那样,我能找到的唯一参考是模糊的,但是在解释它时我会倾向于我最初的假设。

我当然希望这种行为没有什么不同从编译器到

编译器!


谢谢。

I have a few std::strings that I am using to store raw binary data,
each of which may very well include null bytes at any point or
points. I want to slap them together into a single string, so I tried
a std::ostringstream:

std::ostringstream oss;
oss << x << y << z;
std::string result ( oss.str() );

The result shows that feeding the ostringstream with a string just
takes the string''s data up to (and not including) the first null
character.

Stroustrop''s book is vague about what should happen (at least in the
sole reference I could find); it merely says "The << operator writes a
string to an ostream".

Obviously I could just concatenate the strings using the + operator.
But I''m wondering - is there some other kind of stream that is
supposed to be used for the purpose I want? Or some stream
manipulator?

And I''m also wondering whether the observed behavior is correct in the
first place. Like I said, the sole reference I could find is vague,
but I would lean towards my original assumption when interpreting it.
And I certainly hope the behavior is not different from compiler to
compiler!

Thanks.

推荐答案

5月29,4:36 pm,coomberjo ... @ gmail.com写道:
On May 29, 4:36 pm, coomberjo...@gmail.com wrote:

我有几个用于存储原始二进制数据的std :: strings ,

每个可能在任何点都包含空字节或

点。我想把它们一起打成一个字符串,所以我试过了

a std :: ostringstream:


std :: ostringstream oss;

oss<< x<< y<< z;

std :: string result(oss.str());


结果显示用字符串输入ostringstream只是

将字符串的数据带到(并且不包括)第一个空的

字符。


Stroustrop的书很模糊关于应该发生什么(至少在我能找到的唯一参考资料中);它只是说<<<<<<<运算符将一个

字符串写入ostream。


显然我可以使用+运算符连接字符串。

但是我在想 - 是否还有其他类型的流?

应该用于我想要的目的?或者一些流

操纵者?


我也想知道观察到的行为是否正确在

第一名。就像我说的那样,我能找到的唯一参考是模糊的,但是在解释它时我会倾向于我最初的假设。

我当然希望这种行为没有什么不同从编译器到

编译器!


谢谢。
I have a few std::strings that I am using to store raw binary data,
each of which may very well include null bytes at any point or
points. I want to slap them together into a single string, so I tried
a std::ostringstream:

std::ostringstream oss;
oss << x << y << z;
std::string result ( oss.str() );

The result shows that feeding the ostringstream with a string just
takes the string''s data up to (and not including) the first null
character.

Stroustrop''s book is vague about what should happen (at least in the
sole reference I could find); it merely says "The << operator writes a
string to an ostream".

Obviously I could just concatenate the strings using the + operator.
But I''m wondering - is there some other kind of stream that is
supposed to be used for the purpose I want? Or some stream
manipulator?

And I''m also wondering whether the observed behavior is correct in the
first place. Like I said, the sole reference I could find is vague,
but I would lean towards my original assumption when interpreting it.
And I certainly hope the behavior is not different from compiler to
compiler!

Thanks.



std :: strings是为std :: stringstream设计的,因此你的第一个问题就在于你想要的第一个问题将二进制数据流式传输为

text。我会在继续之前解决这个设计缺陷,否则你只需要b / b
来解决这个问题。


你的第二个问题,可能会也可能不会是,你为什么要_stream
到_stream_二进制数据?是否有必要?

我能想到某些情况,但它可能不在你的

的情况下。


如果你看一下iostream层次结构,你可以发现你的问题。


根据你的二进制数据代表什么,你要用什么做什么?
它,以及你想要你的解决方案有多复杂,我会使用不同的现有iostream类型调查

或通过

派生来设计我自己的。




< co ********** @gmail.comaécritdansle message de news:
87 ********* ... oglegroups.com ...

<co**********@gmail.coma écrit dans le message de news:
87**********************************...oglegroups.com...

>我有几个用于存储原始二进制数据的std :: strings,

每个都可能很好在任何点包含空字节或

点。我想把它们一起打成一个字符串,所以我试过了

a std :: ostringstream:


std :: ostringstream oss;

oss<< x<< y<< z;

std :: string result(oss.str());


结果显示用字符串输入ostringstream只是

将字符串的数据带到(并且不包括)第一个空的

字符。


Stroustrop的书很模糊关于应该发生什么(至少在我能找到的唯一参考资料中);它只是说<<<<<<<运算符将一个

字符串写入ostream。


显然我可以使用+运算符连接字符串。

但是我在想 - 是否还有其他类型的流?

应该用于我想要的目的?或者一些流

操纵者?


我也想知道观察到的行为是否正确在

第一名。就像我说的那样,我能找到的唯一参考是模糊的,但是在解释它时我会倾向于我最初的假设。

我当然希望这种行为没有什么不同从编译器到

编译器!


谢谢。
>I have a few std::strings that I am using to store raw binary data,
each of which may very well include null bytes at any point or
points. I want to slap them together into a single string, so I tried
a std::ostringstream:

std::ostringstream oss;
oss << x << y << z;
std::string result ( oss.str() );

The result shows that feeding the ostringstream with a string just
takes the string''s data up to (and not including) the first null
character.

Stroustrop''s book is vague about what should happen (at least in the
sole reference I could find); it merely says "The << operator writes a
string to an ostream".

Obviously I could just concatenate the strings using the + operator.
But I''m wondering - is there some other kind of stream that is
supposed to be used for the purpose I want? Or some stream
manipulator?

And I''m also wondering whether the observed behavior is correct in the
first place. Like I said, the sole reference I could find is vague,
but I would lean towards my original assumption when interpreting it.
And I certainly hope the behavior is not different from compiler to
compiler!

Thanks.



如果你不想,你可能要考虑使用bitset;需要动态地改变你的容器的大小。它特别希望保存二进制

数据。


如果你需要迭代器(bitset不具备的东西),另一种选择是

deque< boolwhich是一个STL容器。但底层记忆并不是
连续。


最后还有vector< bool但是那个有2个问题:

1.它不是STL容器

2.它不包含bool(顺便说一下deque< boolreally hold bool)


Eric Pruneau

You may want to consider using bitset if you don;t need to dynamically
change the size of your container. It is specifically desing to hold binary
data.

If you need iterator(something that bitset doesn''t have), an other option is
deque<boolwhich is an STL container. But the underlying memory isn''t
contiguous.

Finally there is vector<boolbut there is 2 problems with that one:
1. it is not an STL container
2. it doesn''t contain bool (by the way deque<boolreally hold bool)

Eric Pruneau


5月29日晚上11点36分,coomberjo ... @ gmail.com写道:
On May 29, 11:36 pm, coomberjo...@gmail.com wrote:

我有一些std :: strings用于存储原始二进制数据
数据,每个数据都可能包含任何

点或点的空字节。
I have a few std::strings that I am using to store raw binary
data, each of which may very well include null bytes at any
point or points.



正如其他人所指出的那样,这可能是一个设计错误。

但是......

As others have pointed out, that''s probably a design error.
However...


我想把它们一起打成一个字符串,所以我试过了

a std :: ostringstream:
I want to slap them together into a single string, so I tried
a std::ostringstream:



和连接将更加合理。

And concatenation would be a lot more reasonable.


std :: ostringstream oss;

oss<< x<< y<< z;

std :: string result(oss.str());
std::ostringstream oss;
oss << x << y << z;
std::string result ( oss.str() );


结果显示用字符串

输入ostringstream只需将字符串的数据提取到(和不包括)

第一个空字符。
The result shows that feeding the ostringstream with a string
just takes the string''s data up to (and not including) the
first null character.



有什么实现?就ostringstream和字符串

而言,''\'''只是一个字符,就像任何其他字符一样。我只是

对四种不同的实现进行了快速测试,而且我不能找到一个无法正常工作的b $ b。 (IIRC,VC ++ 6.0在字符串中出现了''\ 0''的问题。但是它们通常会导致程序崩溃。但当然,没有人使用过这样的问题。一个旧的

编译器。)

With what implementation? As far as ostringstream and string
are concerned, ''\0'' is just a character, like any other. I just
did a quick test on four different implementations, and I can''t
find one where this doesn''t work correctly. (IIRC, VC++ 6.0 had
some problems with ''\0'' in strings. But they generally resulted
a program crash. And of course, no one uses such an old
compiler.)


Stroustrop的书很模糊应该发生什么(至少

我可以找到的唯一参考);它只是说<<<

运算符将字符串写入ostream。
Stroustrop''s book is vague about what should happen (at least
in the sole reference I could find); it merely says "The <<
operator writes a string to an ostream".



还应该说什么?


请注意,任意ostream可能无法处理''\ 0'';

在文本模式下打开一个流,例如,只需要

来处理可打印字符和一小组控件

个字符(''\ n'',''\ t''等),其中一些(例如''\ n'')可能

有特殊行为。但是ostringstream可以处理任何
a字符串可以处理的内容,字符串显然可以处理''\0''。

What more should it say?

Note that an arbitrary ostream may not be able to handle a ''\0'';
an ofstream opened in text mode, for example, is only required
to handle printable characters and a small set of control
characters (''\n'', ''\t'', etc.), and some of these (e.g. ''\n'') may
have special behavior. But an ostringstream can handle anything
a string can handle, and a string can obviously handle ''\0''.


显然我可以使用+

运算符连接字符串。但我想知道 - 是否有其他类型的b / b $ b流应该用于我想要的目的?或者

一些流操纵器?
Obviously I could just concatenate the strings using the +
operator. But I''m wondering - is there some other kind of
stream that is supposed to be used for the purpose I want? Or
some stream manipulator?



好​​吧,基本的流抽象是文本格式化,所以使用

这里的ostringstream似乎有些滥用。但是没有

,因为你发布的确切代码会失败。

Well, the basic stream abstraction is text formatting, so using
an ostringstream here seems a bit of abuse. But there''s no
reason the exact code you post would fail.


我也想知道观察到的行为是否存在

首先是正确的。就像我说的那样,我可以找到的唯一参考资料是模糊的,但在解释它时我会倾向于我原来的

假设。我当然希望

的行为与编译器和编译器没有什么不同!
And I''m also wondering whether the observed behavior is
correct in the first place. Like I said, the sole reference I
could find is vague, but I would lean towards my original
assumption when interpreting it. And I certainly hope the
behavior is not different from compiler to compiler!



我想知道你是如何确定有一个

问题。你有没有机会使用其他一些功能?

特别对待''\0''?因为很明显在这个

的情况下,''\ 0''只是一个像另一个人的角色,我找不到

的实现但事实并非如此。


-

James Kanze(GABI Software)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l'' école,法国,+ 33(0)1 30 23 00 34

I''m wondering how you actually determined that there was a
problem. Did you, per chance, use some other function which
does treat ''\0'' specially? Because it''s clear that in this
case, ''\0'' is just a character like another, and I can''t find an
implementation where this isn''t the case.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于将字符串输入ostringstream只使用第一个null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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