C++使用 VC10 的 std::string 问题
我在 Windows 7 上使用 C++、Visual Studio 2010 Premium,在我的应用程序中,我有一个 std::string 变量在追加时失败:( ...
情况是,字符串变量包含大量附加在不同的时间,例如第一次添加 7609 个字节,第二次添加 8184 个字节,最后添加 1463 个字节,
std::string str;
long bytesRead;
char buffer[BUFFER_SIZE];
do {
bytesRead = ReadChars(buffer, BUFFER_SIZE -1);
buffer[bytesRead] = 0; // I omitted this line before, but it was in the original code
if (bytesRead > 0)
str += buffer; // I'm already taking care of the char 0 at the end :)
} while (bytesRead > 0);
每次我调用 += (或附加,相同的结果)时,它都会附加一个额外的字符串(原始缓冲区中的最后 7 或 8 个字符)例如
原始缓冲区结束 “追加字符串”
追加后,str结束 “字符串附加附加”
有谁知道这是否是一个已知问题?或者也许如果我错过了一些东西,
我会把它放在注释中,但如果我也把它放在这里也许会更好
ReadChars:读取一堆字符并返回读取的字节数
bytesRead< /strong>:如果读的话,它是> 0...所以可以将缓冲区视为 ASCIIZ,
我尝试使用追加函数但获得相同的行为(像这样 str.append(buffer); )
问候 豪尔赫
I'm using C++, Visual Studio 2010 Premium on Windows 7, and in my application I have a std::string variable that fails on append :( ...
The situation is, the string variable contains a huge amount of chars appended at different times, first time I add for example 7609 bytes, second time 8184 and finally 1463, something like
std::string str;
long bytesRead;
char buffer[BUFFER_SIZE];
do {
bytesRead = ReadChars(buffer, BUFFER_SIZE -1);
buffer[bytesRead] = 0; // I omitted this line before, but it was in the original code
if (bytesRead > 0)
str += buffer; // I'm already taking care of the char 0 at the end :)
} while (bytesRead > 0);
so every time I call += (or append, same result) it append an extra string (last 7 or 8 chars from original buffer) for instance
original buffer ends
"string appended"
after append, str ends
"string appendedappended"
does anyone knows if this is a known issue? or maybe if I missed something
I put this into the comments down there, but maybe better if I put it here too
ReadChars: read a bounch of chars and return the number of bytes read
bytesRead: if read, it is > 0... so is ok to treat buffer as ASCIIZ,
I tried with append function but obtain same behavior ( like this str.append(buffer); )
Regards
Jorge
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这段代码是您的实际代码吗? “照顾 char 0”是什么意思?
在您的示例中,您始终将整个缓冲区附加到字符串,从而有效地忽略
bytesRead
。因此,std::string 将从 buffer 中读取,直到找到 NULL 字节——这可能会导致溢出,而且肯定不是您想要的。您必须告诉从
buffer
中追加多少字节。请尝试以下方法:Is this snippet your actual code? What do you mean with "taking care of the char 0"?
In your example, you always append the whole buffer to the string, effectively ignoring
bytesRead
. Sostd::string
will read frombuffer
until a NULL byte is found -- which could cause overflows and is certainly not what you want.You have to tell how many bytes from
buffer
to append. Try the following instead:您说您已经处理了末尾的“0”...您确定没有从您尝试附加的每个字符串的末尾删除空格字符吗?
You said you already took care of the "0" at the end... are you sure you didn't remove the space character from the end of each string you are trying to append?
如果没有有关
ReadChars
的信息,这看起来完全就像buffer
不会以 null 终止并且std::string
正在附加从缓冲区中提取字符,直到它在内存中遇到随机 0。Without information about
ReadChars
this looks exactly likebuffer
doesn't get null terminated andstd::string
is appending character from the buffer until it hits a random 0 in memory.