請不要把這個釘在十字架上。我決定使用char *可能會很好,因爲我打算構建的字符串的大小已知。我也知道,如果timeinfo-> tm_hour返回2位數以外的內容,那麼事情就會發生嚴重錯誤。也就是說,當這個函數返回時,VIsual Studio會對我進行HEAP CORRUPTION猿人的訓練。出了什麼問題? (另外,我應該只是使用StringBuilder?)爲什麼我會「堆腐敗」?
void cLogger::_writelogmessage(std::string Message)
{
time_t rawtime;
struct tm* timeinfo = 0;
time(&rawtime);
timeinfo = localtime(&rawtime);
char* MessageBuffer = new char[Message.length()+11];
char* msgptr = MessageBuffer;
_itoa(timeinfo->tm_hour, msgptr, 10);
msgptr+=2;
strcpy(msgptr, "::");
msgptr+=2;
_itoa(timeinfo->tm_min, msgptr, 10);
msgptr+=2;
strcpy(msgptr, "::");
msgptr+=2;
_itoa(timeinfo->tm_sec, msgptr, 10);
msgptr+=2;
strcpy(msgptr, " ");
msgptr+=1;
strcpy(msgptr, Message.c_str());
_file << MessageBuffer;
delete[] MessageBuffer;
}
好像你忘記了字符串末尾的NUL。關閉一個。 – jrockway 2010-04-19 07:56:27
@jrockway不會在Message.c_str()中添加空字符? – User2400 2010-04-19 07:58:28
'/我準備釘子和錘子' – 2010-04-19 07:59:46