这是我到目前为止的代码,它找到了根的位置,但是当我添加该行时:
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;
}