문제

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
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top