我正在编写一个简单的C程序,它接收一个目录作为参数,并显示该目录中的文件以及他的子目录.为此我写了一个"递归"函数.但由于一个未知的原因,我的程序在该stat功能失败.这是我的计划:
#define _POSIX_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
void display_directory(char* path){
DIR* directory = opendir(path);
if( directory == NULL){
printf("opendir failure for %s\n", path);
exit(1);
}
struct dirent* dirent;
struct stat stat_info;
while((dirent = readdir(directory)) != NULL){
printf("[%s]\n", dirent->d_name);
if(stat(dirent -> d_name, &stat_info) == -1){
printf("readdir error for %s\n", dirent->d_name);
exit(1);
}
if(S_ISREG(stat_info.st_mode)){
printf("File: %s \n", dirent -> d_name);
}
if(S_ISDIR(stat_info.st_mode)){
if(strncmp(dirent->d_name, "..",2)){
printf("Directory : %s\n", dirent->d_name);
display_directory(dirent->d_name);
}
}
}
closedir(directory);
}
int main(int argc, char* argv[]){
char* path;
if(argc > 1){
path = argv[1];
} else {
path = ".";
}
display_directory(path);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
例如,如果在我的目录A中,我有a1,a2,a3和..,它首先读取..目录,当它读取目录a1时,该stat函数失败.
有人可以告诉我我的代码不正确.
[编辑]我包括<errno.h>你们许多建议,并在运行程序后,我有错误Too many open files.
#define _POSIX_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
void display_directory(char* path){
DIR* directory = opendir(path);
if( directory == NULL){
printf("opendir failure for %s --> %s\n", path, strerror(errno));
exit(1);
}
struct dirent* dirent;
struct stat stat_info;
while((dirent = readdir(directory)) != NULL){
printf("[%s]\n", dirent->d_name);
if(stat(dirent->d_name, &stat_info)){
printf("readdir error for %s ---> %s\n", dirent->d_name, strerror(errno));
continue;
}
if(S_ISREG(stat_info.st_mode)){
printf("Fichier : %s \n", dirent->d_name);
}
if(S_ISDIR(stat_info.st_mode)){
if(strncmp(dirent->d_name, "..",2)){
printf("Directory : %s\n", dirent->d_name);
display_directory(dirent->d_name);
}
}
}
closedir(directory);
}
int main(int argc, char* argv[]){
char* path;
if (argc > 2) {
fprintf(stderr, "Usage: %s [directory]\n", argv[0]);
exit(1);
}
path = argv[1];
display_directory(path);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
该计划的输出:
[..]
[mykill.c]
readdir error for mykill.c ---> No such file or directory
[.]
Directory : .
[..]
[.]
Directory : .
[..]
[.]
Directory : .
[..]
...
...
Directory : .
opendir failure for . --> Too many open files
Run Code Online (Sandbox Code Playgroud)
mykill.c是作为参数传递的目录中的文件.
我非常清楚错误,但我想先告诉你如何为自己调试这个问题.更改此代码...
if(stat(dirent -> d_name, &stat_info) == -1){
printf("readdir error for %s\n", dirent->d_name);
exit(1);
}
Run Code Online (Sandbox Code Playgroud)
......改为阅读......
if (stat(dirent->d_name, &stat_info)) {
printf("%s: %s\n", dirent->d_name, strerror(errno));
continue;
}
Run Code Online (Sandbox Code Playgroud)
您需要添加到包含列表
#include <errno.h>
Run Code Online (Sandbox Code Playgroud)
再次运行该程序.如果你没有从输出中看到问题是什么,那么在你的问题中编辑COMPLETE,UNEDITED输出,我们将从那里开始.