Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中的strftime警告错误_C_File_File Io_Time - Fatal编程技术网

C中的strftime警告错误

C中的strftime警告错误,c,file,file-io,time,C,File,File Io,Time,我在.c文件中有一个方法,返回文件的修改时间 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", t

我在.c文件中有一个方法,返回文件的修改时间

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);
}
但是我得到了这个编译警告,我该如何修复它

client/file_monitor.c:227:5: warning: ISO C does not support the '%s' gnu_strftime format [-Wformat=]
 strftime(datestring, sizeof(datestring), "%s", tm);
 ^
根据这一点(以及您的错误):


%s
不是受支持的格式选项。我不确定您使用
%s
的目的是什么,但可能
%s
正是您想要的。

这应该是一种更复杂、效率更低的写作方式:

int lastModifiedTime(const char *filePath)
{
    struct stat attrib;
    if (stat(filePath, &attrib) != 0)
        return -1;
    return attrib.st_mtime;
}
然而,要回答这个问题:

  • 当您指定
    -pedantic
    时会出现错误(即使显示了所有其他严格的警告,当您不指定时也不会出现错误):

  • 如上所述,通过省略
    -pedantic
    ,可以避免该错误。假设需要
    -pedantic
    ,那么你似乎被套住了。我能得到的最接近的方法是避免错误,但仍然得到警告:

    $ 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);
         ^
    $
    
  • 我可能缺乏想象力,但我无法抑制这种警告。我尝试过(在Ubuntu 14.04上使用GCC4.8.2):

    • -Wnoformat
    • -Wno格式
    • -Wno格式=
    • -Wnoerror=格式=
    • -Wnoformat=
    但这些都没有被接受


您正在使用gcc吗?你给它什么命令行选项?是否要使用“%s”`说明符(该说明符提供自1970年以来的秒数)?是的,我确实需要epoch-times和我问的其他问题?我正在尝试获取epoch-time
$ 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);
     ^
$