小编Jor*_*rge的帖子

使用VC10的C++ 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); 
Run Code Online (Sandbox Code Playgroud)

所以每当我调用+ =(或追加,相同的结果)时,它会附加一个额外的字符串(例如,来自原始缓冲区的最后7或8个字符)

原始缓冲区结束 "附加字符串"

在追加之后,str结束 "string attacheddpended"

有谁知道这是一个已知的问题?或者如果我错过了什么

我把它放在那里的评论中,但如果我把它放在这里也许会更好

ReadChars:读取一个字符的bounch并返回读取的字节数

bytesRead:如果读取,则> 0 ...所以可以将缓冲区视为ASCIIZ,

我尝试使用append函数但获得相同的行为(如此str.append(buffer);)

关心豪尔赫

c++ string visual-studio-2010 windows-7

0
推荐指数
1
解决办法
697
查看次数

标签 统计

c++ ×1

string ×1

visual-studio-2010 ×1

windows-7 ×1