我有这个从串口读取数据的程序。对于每一行,我试图将当前时间与数据行连接起来。出于某种原因,它在第二次打印时崩溃(它似乎在括号的末尾?)。奇怪的是,如果我注释打印出来,它仍然会崩溃
char * cdata;
{
if( BINARY_ASCII == 1 ) //right now this is just set to 0, please ignore
{
cdata = convertBSTRToByteArray(data , numChars);
}
else
{
cdata = convertBSTRToString(data);
//prints the original output
cout << "before timestamp concat is: " << cdata << "\n";
//this is supposed to concatenate each output line with the associated time
std::stringstream ss;
ss << currentDateTime() << "," << cdata;
std::string s = ss.str();
std::strcpy(cdata,s.c_str());
cout << "after timestamp concat is: " << cdata << "\n"; //around here it crashes
}
cout << "after the thing" << "\n"; //does not even get here
我认为 char * 数据会是问题,但我尝试过像这样初始化它
char *cdata = 0;
和
char *cdata = new char [100];
没有改变...
这让我觉得我在连接中做错了什么?