gpt4 book ai didi

c - 使用 Stat 和 OpenDir()/ReadDir() 在 C 语言中列出当前目录和文件类型(Dir、Reg、Lnk)

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:57:56 25 4
gpt4 key购买 nike

您好,感谢阅读。

我正在尝试用 C 构建一个在 Linux Shell 中运行的程序,它接受一个参数(目录)并列出该目录中的所有文件,以及它们是常规文件、链接文件还是目录.我需要使用 OpenDir()/ReadDir() 和 stat。到目前为止,这是我的代码:(我有点卡住了)目前它只读取一个文件并输出类型。我如何允许我的程序读取目录中的所有文件,并输出它们的名称以及它们是什么类型的文件?

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

int
main(int argc, char *argv[])
{
struct stat sb;

/*
if (argc != 2) {
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
exit(EXIT_FAILURE);
}*/

if (stat(argv[1], &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}



switch (sb.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("dir "); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("lnk "); break;
case S_IFREG: printf("reg "); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}

printf("%s\n", argv[1]);

exit(EXIT_SUCCESS);
}

所以我重写了所有内容,现在我收到一些关于它是链接 reg 还是 dir 文件的输出,但是我只收到一个文件是目录,即使它显然不是(文本文件或 . c 文件等)。这是新代码:

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


void typeOfFile(struct stat info){

if (S_ISREG(info.st_mode)) {
printf("reg");
}
if (S_ISDIR(info.st_mode)) {
printf("dir");
}
if (S_ISLNK(info.st_mode)) {
printf("lnk");
}

}

int main(int argc, char *argv[]) {

DIR *dirp;
struct dirent* dent;
struct stat info;







typeOfFile(info);

//If no args
if(argc == 1){

argv[1] = ".";
dirp=opendir(argv[1]); // specify directory here: "." is the "current directory"
do {
dent = readdir(dirp);
if (dent)
{
//////////////////////////////////////////////////////////////////////////
if (stat(argv[1], &info) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}



switch (info.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("dir "); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("lnk "); break;
case S_IFREG: printf("reg "); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}


//////////////////////////////////////////////////////////////////////////
printf("%s \n", dent->d_name);

}
} while (dent);
closedir(dirp);

}



////////////////////////////////////////////////////////////////////////////////////////////////
//If specified directory
if(argc > 1){
dirp=opendir(argv[1]); // specify directory here: "." is the "current directory"
do {
dent = readdir(dirp);
if (dent)
{

/////////////////////////////////////////////////////////////////////////////////////
if (stat(argv[1], &info) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}



switch (info.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("dir "); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("lnk "); break;
case S_IFREG: printf("reg "); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
//////////////////////////////////////////////////////////////////////////////////////
// printf("%s\n", argv[1]);


printf("%s \n", dent->d_name);

}
} while (dent);
closedir(dirp);

}
return 0;
}

最佳答案

您的代码正在使用始终为“.”的 argv[1] 调用 stat - 因此所有项目都被错误地识别为目录。

dent->d_name(文件名)是这里的正确参数。

  if (stat(dent->d_name, &info) == -1) {

关于c - 使用 Stat 和 OpenDir()/ReadDir() 在 C 语言中列出当前目录和文件类型(Dir、Reg、Lnk),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23969645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com