0

这是我到目前为止的代码,它找到了根的位置,但是当我添加该行时:

printf("        name: %s\n", readdir(opendir(cur_spot))->d_name);

它改变了 cur_spot 并向其添加了奇怪的字符(文件名:.~?)是它打印的内容。知道为什么会这样吗?

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

int main(int argc, char *argv[]) {
    struct stat file_stats;
    struct stat parent_stats;
    struct dirent temp_dir;

    char cwd_name[256]; //directory name
    char cur_spot[256]; //current directory spot

    cur_spot[0] = '.';
    stat(cur_spot, &file_stats);
    printf("filename: %s\n", cur_spot);
    printf(" inode: %ld\n", file_stats.st_ino);


    strcat(cur_spot, ".");
    stat(cur_spot, &parent_stats);
    printf("filename: %s\n", cur_spot);
    printf(" inode: %ld\n", parent_stats.st_ino);

    while(file_stats.st_ino != parent_stats.st_ino) {
        printf("not at root yet\n\n");

        stat(cur_spot, &file_stats);
        printf("    current child\n");
        printf("        inode: %ld\n", file_stats.st_ino);
        printf("        name: %s\n", readdir(opendir(cur_spot))->d_name);
        strcat(cur_spot, "/..");
        stat(cur_spot, &parent_stats);
        printf("    current parent\n");
        printf("        inode: %ld\n", parent_stats.st_ino);
    }
        printf("at root\n");


    return 0;
}
4

4 回答 4

1

这在 GCC 上对我来说很好:

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

int main(int argc, char *argv[]) {
    struct stat file_stats;
    struct stat parent_stats;
    struct dirent temp_dir;
    DIR *dirp;

    char cwd_name[256]; //directory name
    char cur_spot[256]; //current directory spot

    strcpy(cur_spot,".");  // <------------ changed
    stat(cur_spot, &file_stats);
    printf("filename: %s\n", cur_spot);
    printf(" inode: %ld\n", file_stats.st_ino);


    strcat(cur_spot, ".");
    stat(cur_spot, &parent_stats);
    printf("filename: %s\n", cur_spot);
    printf(" inode: %ld\n", parent_stats.st_ino);

    while(file_stats.st_ino != parent_stats.st_ino) {
        printf("not at root yet\n\n");

        stat(cur_spot, &file_stats);
        printf("    current child\n");
        printf("        inode: %ld\n", file_stats.st_ino);
        dirp=opendir(cur_spot); // <----------------added
        printf("        name: %s\n", readdir(dirp)->d_name);  // <----changed
        closedir(dirp); // <------------------------added
        strcat(cur_spot, "/..");
        stat(cur_spot, &parent_stats);
        printf("    current parent\n");
        printf("        inode: %ld\n", parent_stats.st_ino);
    }
        printf("at root\n");


    return 0;
}
于 2012-12-07T01:05:47.670 回答
0

您应该在字符数组的末尾添加一个 NULL 字符。

错误的:

cur_spot[0] = '.';

对:

cur_spot[0] = '.';
cur_spot[1] = '\0';

NULL 字符终止一个字符串。默认情况下,字符串可能会或可能不会初始化为 NULL。

于 2012-12-07T01:09:50.867 回答
0

你在一个循环中做了一堆“opendir”,但我没有看到“closedir”的痕迹。你应该受到惩罚...

于 2012-12-07T01:09:51.163 回答
0

这超出了您最初问题的范围,因此我将其作为第一个问题的单独答案添加。正如我在我的第一个答案下的评论中所说,如果您要打印目录中所有文件的文件名,您只需继续调用 readdir() 直到某个终止条件,从阅读手册页来看,这只是一个 NULL 返回价值。顺便说一句,如果您的手册页显示READDIR(2)or This is not the function you are interested in,您真正想要做的是查看 readdir 上的第 3 节手册页,您可以使用man -s 3 readdir.

无论如何,我修改了我的第一个答案中的代码来做我认为你想要的:

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

int main(int argc, char *argv[]) {
    struct stat file_stats;
    struct stat parent_stats;
    struct dirent temp_dir;
    DIR *dirp;
    struct dirent* dent;

    char cwd_name[256]; //directory name
    char cur_spot[256]; //current directory spot

    strcpy(cur_spot,".");          // <------------- changed
    stat(cur_spot, &file_stats);
    printf("filename: %s\n", cur_spot);
    printf(" inode: %ld\n", file_stats.st_ino);


    strcat(cur_spot, ".");
    stat(cur_spot, &parent_stats);
    printf("filename: %s\n", cur_spot);
    printf(" inode: %ld\n", parent_stats.st_ino);

    while(file_stats.st_ino != parent_stats.st_ino) {
        printf("not at root yet\n\n");

        stat(cur_spot, &file_stats);
        printf("    current child\n");
        printf("        inode: %ld\n", file_stats.st_ino);
        dirp=opendir(cur_spot);    // <------------- added
        do {                       // <------------- NEW
            dent = readdir(dirp);  // <------------- NEW
            if (dent)              // <------------- NEW
                printf("        name: %s\n", dent->d_name);
        } while (dent);            // <------------- NEW
        closedir(dirp);            // <------------- added
        strcat(cur_spot, "/..");
        stat(cur_spot, &parent_stats);
        printf("    current parent\n");
        printf("        inode: %ld\n", parent_stats.st_ino);
    }
        printf("at root\n");


    return 0;
}
于 2012-12-07T15:46:52.367 回答