Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.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
C 遍历目录并打印一些信息_C_Stat - Fatal编程技术网

C 遍历目录并打印一些信息

C 遍历目录并打印一些信息,c,stat,C,Stat,我想写一个C程序,它以文件夹的路径作为参数,并显示它包含的文件的一些信息 到目前为止,我写了以下内容: #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <stdio.h> #include <string.h> int main(int argc, char** argv){ char* dir_path = argv[1];

我想写一个C程序,它以文件夹的路径作为参数,并显示它包含的文件的一些信息

到目前为止,我写了以下内容:

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

int main(int argc, char** argv){
    char* dir_path = argv[1];
    char* dir_path_bar = strcat(dir_path, "/");
    DIR* dir = opendir(dir_path);

    for(struct dirent* entry = readdir(dir); entry != NULL; entry = readdir(dir)){
        printf("Next entry is %s\n", entry->d_name);
        char* entry_path = strcat(dir_path_bar, entry->d_name);
        printf("%s\n", entry_path);
        struct stat buf;
        stat(entry_path, &buf);
        printf("Its inode number is %s\n", entry->d_ino);
        printf("Its inode number is %s\n", buf.st_ino);
        printf("Its uid is %s\n", buf.st_uid);
        printf("Its size is %s bytes\n", buf.st_size);
    };
    closedir(dir);
}
#包括
#包括
#包括
#包括
#包括
int main(int argc,字符**argv){
char*dir_path=argv[1];
char*dir\u path\u bar=strcat(dir\u path,“/”);
DIR*DIR=opendir(DIR\u路径);
for(struct dirent*entry=readdir(dir);entry!=NULL;entry=readdir(dir)){
printf(“下一个条目是%s\n”,条目->数据单元名称);
char*entry\u path=strcat(dir\u path\u栏,entry->d\u name);
printf(“%s\n”,条目路径);
结构统计buf;
统计(输入路径和buf);
printf(“其inode编号为%s\n”,条目->d\u ino);
printf(“其inode编号为%s\n”,buf.st_ino);
printf(“它的uid是%s\n”,buf.st\u uid);
printf(“其大小为%s字节\n”,buf.st\u大小);
};
closedir(dir);
}
它可以编译,但是
stat
调用给了我一个SEGFAULT。发生了什么事?

两个问题:

  • 您不断地向输入(
    argv[1]
    )参数追加未定义的行为。您不能附加到
    argv
    的字符串

  • 还可以使用未定义的
    %s
    打印整数值
    %
    s需要一个
    char*
    参数,但您想要打印整数值

您可以改为使用临时缓冲区并将其传递给:

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
如果(argc!=2){
printf(“用法:%s dir\n”,argv[0]);
出口(1);
}
char*dir_path=argv[1];
DIR*DIR=opendir(DIR\u路径);
如果(!dir){
perror(“opendir”);
出口(1);
}
for(struct dirent*entry=readdir(dir);entry!=NULL;entry=readdir(dir)){
char entry_path[path_MAX]={0};
int rc=snprintf(条目路径,条目路径大小,“%s/%s”,目录路径,条目->数据名称);
if(rc<0 | | rc>=sizeof entry|u path){
fprintf(stderr,“为“%s”截断的路径,\n”,条目->数据单元名称);
继续;
}
printf(“下一个条目是:%s\n”,条目路径);
结构统计buf;
if(stat(条目路径和buf)==0){
printf(“其inode编号为%ju\n”,(uintmax\u t)条目->d\u ino);
printf(“其inode编号为%ju\n”,(uintmax\u t)buf.st\u ino);
printf(“它的uid是%jd\n”,(intmax\t)buf.st\u uid);
printf(“其大小为%jd字节\n”,(intmax_t)buf.st_大小);
}否则{
佩罗(“stat”);
}
}
closedir(dir);
}
我还添加了一些错误检查。

两个问题:

  • 您不断地向输入(
    argv[1]
    )参数追加未定义的行为。您不能附加到
    argv
    的字符串

  • 还可以使用未定义的
    %s
    打印整数值
    %
    s需要一个
    char*
    参数,但您想要打印整数值

您可以改为使用临时缓冲区并将其传递给:

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
如果(argc!=2){
printf(“用法:%s dir\n”,argv[0]);
出口(1);
}
char*dir_path=argv[1];
DIR*DIR=opendir(DIR\u路径);
如果(!dir){
perror(“opendir”);
出口(1);
}
for(struct dirent*entry=readdir(dir);entry!=NULL;entry=readdir(dir)){
char entry_path[path_MAX]={0};
int rc=snprintf(条目路径,条目路径大小,“%s/%s”,目录路径,条目->数据名称);
if(rc<0 | | rc>=sizeof entry|u path){
fprintf(stderr,“为“%s”截断的路径,\n”,条目->数据单元名称);
继续;
}
printf(“下一个条目是:%s\n”,条目路径);
结构统计buf;
if(stat(条目路径和buf)==0){
printf(“其inode编号为%ju\n”,(uintmax\u t)条目->d\u ino);
printf(“其inode编号为%ju\n”,(uintmax\u t)buf.st\u ino);
printf(“它的uid是%jd\n”,(intmax\t)buf.st\u uid);
printf(“其大小为%jd字节\n”,(intmax_t)buf.st_大小);
}否则{
佩罗(“stat”);
}
}
closedir(dir);
}

我还添加了一些错误检查。

正如其他人提到的,您不能附加到
argv[1]
。你不能一直在循环中附加它。而且,您不能使用
%s
输出数字

这是您的代码,其中已注释并修复了错误[使用
#if 0
显示旧代码]:

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

int
main(int argc, char **argv)
{
    char *dir_path = argv[1];

// NOTE/BUG: argv[1] has a fixed size you can't append to it
#if 0
    char *dir_path_bar = strcat(dir_path, "/");
#else
    char dir_path_bar[PATH_MAX];
    strcpy(dir_path_bar,dir_path);
    strcat(dir_path_bar,"/");
#endif

    DIR *dir = opendir(dir_path);
#if 1
    if (dir == NULL) {
        perror(dir_path);
        exit(1);
    }
#endif

    for (struct dirent *entry = readdir(dir); entry != NULL;
        entry = readdir(dir)) {
        printf("Next entry is %s\n", entry->d_name);

// NOTE/BUG: because you don't reset dir_path_bar, this just keeps appending
// to it
#if 0
        char *entry_path = strcat(dir_path_bar, entry->d_name);
#else
        char entry_path[PATH_MAX];
        strcpy(entry_path,dir_path_bar);
        strcat(entry_path,entry->d_name);
#endif

        printf("\n");
        printf("%s\n", entry_path);
        struct stat buf;

        stat(entry_path, &buf);

// NOTE/BUG: these need one or more of: %d/%ld/%lld (vs %s)
#if 0
        printf("Its inode number is %s\n", entry->d_ino);
        printf("Its inode number is %s\n", buf.st_ino);
        printf("Its uid is %s\n", buf.st_uid);
        printf("Its size is %s bytes\n", buf.st_size);
#else
        printf("Its inode number is %ld\n", entry->d_ino);
        printf("Its inode number is %ld\n", buf.st_ino);
        printf("Its uid is %d\n", buf.st_uid);
        printf("Its size is %ld bytes\n", buf.st_size);
#endif
    };

    closedir(dir);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
int
主(内部argc,字符**argv)
{
char*dir_path=argv[1];
//注意/错误:argv[1]有一个固定的大小,您不能附加到它
#如果0
char*dir\u path\u bar=strcat(dir\u path,“/”);
#否则
char dir_path_bar[path_MAX];
strcpy(dir\u path\u bar,dir\u path);
strcat(dir\u path\u bar,“/”;
#恩迪夫
DIR*DIR=opendir(DIR\u路径);
#如果1
if(dir==NULL){
佩罗尔(迪鲁路);
出口(1);
}
#恩迪夫
for(struct dirent*entry=readdir(dir);entry!=NULL;
entry=readdir(dir)){
printf(“下一个条目是%s\n”,条目->数据单元名称);
//注意/错误:因为您没有重置dir\u path\u栏,所以它会一直追加
//对它
#如果0
char*entry\u path=strcat(dir\u path\u栏,entry->d\u name);
#否则
字符输入路径[路径最大值];
strcpy(入口路径、方向路径栏);
strcat(输入路径,输入->数据名称);
#恩迪夫
printf(“\n”);
printf(“%s\n”,条目路径);
结构统计buf;
统计(输入路径和buf);
//注意/错误:这些需要一个或多个:%d/%ld/%lld(vs%s)
#如果0
printf(“其inode编号为%s\n”,条目->d\u ino);
printf(“其inode编号为%s\n”,buf.st_ino);
printf(“它的uid是%s\n”,buf.st\u uid);
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
    char *dir_path = argv[1];

// NOTE/BUG: argv[1] has a fixed size you can't append to it
#if 0
    char *dir_path_bar = strcat(dir_path, "/");
#else
    char dir_path_bar[PATH_MAX];
    strcpy(dir_path_bar,dir_path);
    strcat(dir_path_bar,"/");
#endif

    DIR *dir = opendir(dir_path);
#if 1
    if (dir == NULL) {
        perror(dir_path);
        exit(1);
    }
#endif

    for (struct dirent *entry = readdir(dir); entry != NULL;
        entry = readdir(dir)) {
        printf("Next entry is %s\n", entry->d_name);

// NOTE/BUG: because you don't reset dir_path_bar, this just keeps appending
// to it
#if 0
        char *entry_path = strcat(dir_path_bar, entry->d_name);
#else
        char entry_path[PATH_MAX];
        strcpy(entry_path,dir_path_bar);
        strcat(entry_path,entry->d_name);
#endif

        printf("\n");
        printf("%s\n", entry_path);
        struct stat buf;

        stat(entry_path, &buf);

// NOTE/BUG: these need one or more of: %d/%ld/%lld (vs %s)
#if 0
        printf("Its inode number is %s\n", entry->d_ino);
        printf("Its inode number is %s\n", buf.st_ino);
        printf("Its uid is %s\n", buf.st_uid);
        printf("Its size is %s bytes\n", buf.st_size);
#else
        printf("Its inode number is %ld\n", entry->d_ino);
        printf("Its inode number is %ld\n", buf.st_ino);
        printf("Its uid is %d\n", buf.st_uid);
        printf("Its size is %ld bytes\n", buf.st_size);
#endif
    };

    closedir(dir);

    return 0;
}
    dir_path_len = strlen(dir_path);
    if (dir_path_len >= PATH_MAX - 1) { return EXIT_FAILURE; } // too long

    char entry_path[PATH_MAX];
    strcpy(entry_path, dir_path);
    strcpy(entry_path + dir_path_len++, "/"); // Can use strcpy() here

    DIR *dir = opendir(dir_path);
    ...
    for (struct dirent *entry = readdir(dir); entry != NULL; entry = readdir(dir)) {
        printf("Next entry is %s\n", entry->d_name);

        entry_len = strlen(entry->d_name);
        if (dir_path_len + entry_len >= PATH_MAX) { 
            continue;
            // or 
            return EXIT_FAILURE; // too long
        }
        strcpy(path + dir_path_len, entry->d_name);  // strcpy(), not strcat()

        printf("\n%s\n", entry_path);

        struct stat buf;
        if (stat(entry_path, &buf) ...
        ...