C中的strftime警告错误
我在.c文件中有一个方法,返回文件的修改时间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
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 -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);
^
$