Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.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
使用dirent时,C根索引节点数不同_C_Stat_Inode_Dirent.h - Fatal编程技术网

使用dirent时,C根索引节点数不同

使用dirent时,C根索引节点数不同,c,stat,inode,dirent.h,C,Stat,Inode,Dirent.h,我已经编写了一个C程序,用于显示从用户指定的根目录开始的文件路径。但是,我在使用opendir/readdir和相应的direntp比较Inode编号时遇到问题。主要是,当我使用它们来编写struct stat时,inode的编号与我不使用它们时得到的编号大不相同 我在每一行旁边都放了评论,说明了每一行的功能,并在这篇文章的底部包含了程序的输出。如果您能帮助解决此问题,我们将不胜感激 int main(int argc, char* argv[]){ int found = 0; //de

我已经编写了一个C程序,用于显示从用户指定的根目录开始的文件路径。但是,我在使用opendir/readdir和相应的direntp比较Inode编号时遇到问题。主要是,当我使用它们来编写struct stat时,inode的编号与我不使用它们时得到的编号大不相同

我在每一行旁边都放了评论,说明了每一行的功能,并在这篇文章的底部包含了程序的输出。如果您能帮助解决此问题,我们将不胜感激

int main(int argc, char* argv[]){
int found = 0;    //declaring stuff
struct stat D; 
struct stat ES; 
struct dirent* direntD; 
struct dirent* direntE; 
DIR* dirp;
char currDir[1024];
char newDir[1024];

stat(argv[1], &D);    //load the starting directory into a stat struct 
printf("loaded %s into D\n", argv[1]);    //prints out the directory that has been inputted
printf("Inode number is %ld\n", (long) D.st_ino);    //prints out the inode number

strcpy(newDir, currDir);    //make the path to the parent directory
strcat(newDir, "/..");

printf("%s\n", currDir);    //print out the starting and parent directories
printf("%s\n", newDir);

dirp = opendir(newDir);    //open the parent
while ((direntE = readdir(dirp)) != NULL){    //read the files in the parent
    printf("%s\n", direntE->d_name);    //print names of files in the parent
        stat(direntE->d_name, &ES);    //load file into stat struct ES
        if(ES.st_dev == D.st_dev){    //compare device numbers
            printf("st_dev are the same\n");    //if true, print this
            if(ES.st_ino == D.st_ino){    //compare inode numbers
                printf("st_ino are the same\n");    //if true, print this
                printf("The child Dir is %s\n", direntE->d_name);    //also print the file/directory in the parent that links to the child
                found = 1;    //variable that says i've found what i want
            } else {
                found = 0;
            }
        } else {
            found = 0;
        }
        printf("%ld\n", (long) ES.st_ino);    //printing these to check they both match
        printf("%ld\n", (long) D.st_ino);

        }
    closedir(dirp);    //close dir
}
终端输出

./pathto /usr (pathto is the name of my program, starting in root/usr)
loaded /usr into D 
Inode number is 131073 
/usr
/usr/..
home
0
131073
root
0
131073
..
st_dev are the same
3408247
131073
usr
st_dev are the same
3408247
131073
.
st_dev are the same
3679535
131073

不,我的答案是正确的。您对dirent中的条目调用stat,但这些名称只是单路径组件,它们本身充当相对路径。statfoo,&buf意味着在当前工作目录中查找foo,并将有关foo的信息存放在bufLook中,以了解在循环迭代之间数字有时是如何保持不变的,例如3480247和3480247。为什么呢?因为统计失败,而你没有检查。它失败了,因为该名称不存在于运行程序的当前目录中。最后,为什么不从struct dirent检索d_ino字段?不需要仅仅为了查找inode编号而统计目录条目。经典定义的Unix目录!将名称映射到索引节点。