문제

I have a method in a .c file that returns the modified time of a file.

int lastModifiedTime(char *filePath)
{
    struct stat attrib;
    stat(filePath, &attrib);
    char datestring[256];
    struct tm *tm = localtime(&attrib.st_mtime);
    strftime(datestring, sizeof(datestring), "%s", tm);
    return atoi(datestring);
}

But I get this compile warning, how do I fix it?

client/file_monitor.c:227:5: warning: ISO C does not support the '%s' gnu_strftime format [-Wformat=]
 strftime(datestring, sizeof(datestring), "%s", tm);
 ^
도움이 되었습니까?

해결책 2

That has to be one of the more convoluted and inefficient ways of writing:

int lastModifiedTime(const char *filePath)
{
    struct stat attrib;
    if (stat(filePath, &attrib) != 0)
        return -1;
    return attrib.st_mtime;
}

However, to answer the question:

  • You get the error when you specify -pedantic (and not when you don't, even with all the other stringent warnings shown):

    $ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror -pedantic -c stt.c
    stt.c: In function ‘lastModifiedTime’:
    stt.c:14:5: error: ISO C does not support the ‘%s’ gnu_strftime format [-Werror=format=]
         strftime(datestring, sizeof(datestring), "%s", tm);
         ^
    cc1: all warnings being treated as errors
    $
    
  • As stated, by omitting the -pedantic, you avoid that error. Assuming -pedantic is required, then you seem to be hosed. The nearest I can get is avoiding an error but still getting a warning:

    $ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -pedantic -Wno-error=format= -c stt.c
    stt.c: In function ‘lastModifiedTime’:
    stt.c:14:5: warning: ISO C does not support the ‘%s’ gnu_strftime format [-Wformat=]
         strftime(datestring, sizeof(datestring), "%s", tm);
         ^
    $
    
  • I'm probably suffering from a lack of imagination, but I can't suppress the warning. I've tried (with GCC 4.8.2 on Ubuntu 14.04):

    • -Wnoformat
    • -Wno-format
    • -Wno-format=
    • -Wnoerror=format=
    • -Wnoformat=

    But none of these was accepted.

다른 팁

According to this (and your error): http://www.cplusplus.com/reference/ctime/strftime/

%s isn't a supported format option. I'm not sure what you're using %s for, but maybe %S is what you're looking for.

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