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
C opendir和readdir以及inode_C_Inode_Opendir_Readdir - Fatal编程技术网

C opendir和readdir以及inode

C opendir和readdir以及inode,c,inode,opendir,readdir,C,Inode,Opendir,Readdir,我已将程序保存在一个文件夹中,其中有5个文件。现在我想打印文件inode编号。以下是我的节目: #include <sys/types.h> #include <dirent.h> #include <stdio.h> #include <libgen.h> #include <stdlib.h> #include <string.h> int main(){ DIR *dir; struct dirent *

我已将程序保存在一个文件夹中,其中有5个文件。现在我想打印文件
inode
编号。以下是我的节目:

#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <libgen.h>
#include <stdlib.h>
#include <string.h>

int main(){
   DIR *dir;
   struct dirent *dp;
    if((dir = opendir(".")) == NULL){
        printf ("Cannot open .");
        exit(1);  
    }
    while ((dp = readdir(dir)) != NULL) {
        printf("%i\n",(*dp).d_ino);
    }
}
那么我有5个文件和8个I节点号?怎么可能呢

编辑: 当我添加到打印此

printf("%i\n",dp->d_name);
printf("%i\n",dp->d_ino);
我得到这个输出

-27246574
251
-27246550
250
-27246526
334
-27246502
254
-27246470
257
-27246438
328
-27246414
274
-27246382
283
所以我认为我的程序在目录中找不到文件?

d\u name是一个字符串:

       struct dirent {
           ino_t          d_ino;       /* inode number */
           off_t          d_off;       /* offset to the next dirent */
           unsigned short d_reclen;    /* length of this record */
           unsigned char  d_type;      /* type of file; not supported
                                          by all file system types */
           char           d_name[256]; /* filename */
       };
因此,您应该使用%s而不是%i打印它:

printf("%s %i\n",dp->d_name, dp->d_ino);

然后你会看到里面有什么。

打印文件名(
dp->d_name
)也会很有启发性。这是因为每个目录中都有
条目。@Ulfalizer我也这么想,但5+2->7仍然缺少一个。也许是一个隐藏的文件@用户:可以在您的应用程序中添加
ls-la
的结果dir@smagnan:是的,dotfile听起来很有道理。或者只是打字错误。:)@user3334375(重新编辑):
%s
用于打印带有
printf()
的字符串,而不是
%i
。您还应该使用
%ld
作为索引节点号,因为它是一个长整数。
printf("%s %i\n",dp->d_name, dp->d_ino);