Printing info of a file/director (inode)

佐手、 提交于 2020-01-06 03:33:07

问题


I want to print some information of a directory. My code is:

#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;
}

I'm getting some warnings:

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]

Arrows show the line of each warning. Can you help? Thanks in advance!


回答1:


I found the answer. These formats are long unsigned int. So it's %lu. Anyway thanks for the help @haccks !




回答2:


To print __off_t you need %jd specifier.



来源:https://stackoverflow.com/questions/22575727/printing-info-of-a-file-director-inode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!