You are not allowing strftime to copy enough characters in the last two cases. 10 and 8 are not enough respectively to store the output plus NULL terminator. That's why the content of the arrays is undefined. You are allowing exactly enough space in the first case.
copying from struct to array through strftime not working
-
10-07-2023 - |
Question
I have this small function that takes that gets the system dateTime and break it further into date and time and store it into three char arrays.
But for first array the copying seems to be fine from the struct but for the rest to it just remains empty. any help plz.
#Define MAXLENGTH 50
int _tmain(int argc, _TCHAR* argv[])
{
if(true){
char sysDateTime [MAXLENGTH];
char sysDate [MAXLENGTH];
char sysTime [MAXLENGTH];
time_t now = time(0);
struct tm tstruct;
tstruct = *localtime(&now);
strftime(sysDateTime, 20, "%d/%m/%Y %X", &tstruct);
strftime(sysDate, 10, "%d/%m/%Y", &tstruct);
strftime(sysTime, 8, "%X", &tstruct);
std::cout<<sysDateTime;
std::cout<<sysDate;
std::cout<<sysTime;
}
getchar();
}
Output:
28/04/2014 15:14:20
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow