【问题标题】:C opendir and readdir and inodeC opendir 和 readdir 和 inode
【发布时间】:2015-06-22 18:37:28
【问题描述】:

我已将我的程序保存在包含 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);
    }
}

这是我的结果

251
250
332
254
257
328
274
283

所以我有 5 个文件和 8 个 i-node 编号?怎么可能?

编辑: 当我添加打印这个

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

所以我认为我的程序在目录中找不到文件?

【问题讨论】:

  • 打印文件名 (dp-&gt;d_name) 也会很有启发性。这是因为每个目录中都有 ... 条目。
  • @Ulfalizer 我也想过,但是 5 + 2 -> 7 所以仍然缺少一个。也许是一个隐藏文件...... @user:也许在你的目录中添加ls -la的结果
  • @smagnan:是的,dotfile 听起来很合理。或者只是一个错字。 :)
  • @user3334375(重新编辑):%s 用于打印带有printf() 的字符串,而不是%i
  • 您还应该使用%ld 作为inode 编号,因为它是一个长整数。

标签: c inode opendir readdir


【解决方案1】:

d_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);

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

【讨论】:

    猜你喜欢
    • 2016-06-21
    • 2021-05-16
    • 2011-05-15
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    相关资源
    最近更新 更多