Why does my comportstr get changed in this code to a garbage value? I don't understand how my char array could change values if nothing about it is altered. There is nothing else between those two print statements.
int main(int argc, char* argv[])
{
char comportstr[10];
sprintf(comportstr,"COM%d",COMPORT_NUM);
if(DEBUGGING) fprintf(stderr,"str is: %s\n", comportstr); //prints str is: COM3
sprintf(curtime , "%s" , currentDateTime());
if(DEBUGGING) fprintf(stderr,"str is: %s\n", comportstr); //prints str is: ;32
}
Here's what currentDateTime does. It doesn't modify comportstr at all.
// Gets current date/time, format is YYYY-MM-DD.HH;mm;ss
const std::string currentDateTime()
{
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
// Visit http://www.cplusplus.com/reference/clibrary/ctime/strftime/
// for more information about date/time format
strftime(buf, sizeof(buf), "%Y-%m-%d.%H;%M;%S", &tstruct);
return buf;
}