【问题标题】:Printing info of a file/director (inode)打印文件/目录(inode)的信息
【发布时间】:2014-04-29 19:43:36
【问题描述】:

我想打印一个目录的一些信息。我的代码是:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>
#include <string.h>

int main (int argc, char *argv[])
{
    struct stat fileStat;
    int fd=0;
    if ( ( fd = open (argv[1] , O_RDONLY) ) == -1){
        perror ( "open " );
        exit (1) ;
    }

    if(fstat(fd, &fileStat)<0) return 1;

    printf("Information for %s\n",argv[1]);
    printf("---------------------------\n");
    --->printf("File Size: \t\t%d bytes\n",fileStat.st_size);
    printf("Number of Links: \t%d\n",fileStat.st_nlink);
    --->printf("File inode: \t\t%d\n",fileStat.st_ino);

    return 0;
}

我收到了一些警告:

inode_test.c:24:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘__off_t’ [-Wformat]

inode_test.c:26:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘__ino_t’ [-Wformat]

箭头显示每个警告的行。你能帮我吗?提前致谢!

【问题讨论】:

    标签: c inode fstat


    【解决方案1】:

    要打印__off_t,您需要%jd 说明符。

    【讨论】:

    • 仍有警告。使用 %td 或 %jd。
    【解决方案2】:

    我找到了答案。这些格式是 long unsigned int。所以是%lu。无论如何感谢@hacks的帮助!

    【讨论】:

    • 作为备注,您可以在“lu”前面(但在 % 之后)添加 t/j 加法说明符
    猜你喜欢
    • 2013-01-24
    • 2012-04-15
    • 2016-08-10
    • 1970-01-01
    • 2017-10-02
    • 2020-03-30
    • 2019-07-15
    • 1970-01-01
    • 2016-08-17
    相关资源
    最近更新 更多