C++使用 VC10 的 std::string 问题

发布于 2024-10-16 20:38:15 字数 973 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坚持沉默 2024-10-23 20:38:15

这段代码是您的实际代码吗? “照顾 char 0”是什么意思?

do { 
   bytesRead = ReadChars(buffer, BUFFER_SIZE); 
   if (bytesRead > 0)
       str += buffer; // I'm already taking care of the char 0 at the end :)
} while (bytesRead > 0);

在您的示例中,您始终将整个缓冲区附加到字符串,从而有效地忽略 bytesRead。因此,std::string 将从 buffer 中读取,直到找到 NULL 字节——这可能会导致溢出,而​​且肯定不是您想要的。

您必须告诉从 buffer 中追加多少字节。请尝试以下方法:

do { 
   bytesRead = ReadChars(buffer, BUFFER_SIZE); 
   if (bytesRead > 0)
       str.append(buffer, buffer + bytesRead);
} while (bytesRead > 0);

Is this snippet your actual code? What do you mean with "taking care of the char 0"?

do { 
   bytesRead = ReadChars(buffer, BUFFER_SIZE); 
   if (bytesRead > 0)
       str += buffer; // I'm already taking care of the char 0 at the end :)
} while (bytesRead > 0);

In your example, you always append the whole buffer to the string, effectively ignoring bytesRead. So std::string will read from buffer 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:

do { 
   bytesRead = ReadChars(buffer, BUFFER_SIZE); 
   if (bytesRead > 0)
       str.append(buffer, buffer + bytesRead);
} while (bytesRead > 0);
蓝眼泪 2024-10-23 20:38:15

您说您已经处理了末尾的“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?

一抹苦笑 2024-10-23 20:38:15

如果没有有关 ReadChars 的信息,这看起来完全就像 buffer 不会以 null 终止并且 std::string 正在附加从缓冲区中提取字符,直到它在内存中遇到随机 0。

Without information about ReadChars this looks exactly like buffer doesn't get null terminated and std::string is appending character from the buffer until it hits a random 0 in memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文