gpt4 book ai didi

c - 循环遍历文件并在 C 中打印文件属性

转载 作者:IT王子 更新时间:2023-10-29 01:21:37 28 4
gpt4 key购买 nike

我是 C 语言编程的新手。我需要这个程序循环遍历文件夹中的所有文件并打印每个文件的这些属性。此时它只打印文件夹的属性。

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

int main(int argc, char *argv[])
{
DIR *dp;
struct stat file_stats;

if (argc != 2) {
fprintf(stderr, "Usage: fstat FILE...\n");
return EXIT_FAILURE;
}

if ((stat(argv[1], &file_stats)) == -1) {
perror("fstat");
return EXIT_FAILURE;
}

dp = opendir("./");
if (dp == NULL) {
perror("couldn't open directory");
return EXIT_FAILURE;
}

while (readdir(dp)) {
printf("filename: %s\n", argv[1]);
printf(" device: %lld\n",
file_stats.st_dev);
printf(" protection: %o\n",
file_stats.st_mode);
printf(" number of hard links: %d\n",
file_stats.st_nlink);
printf(" user ID of owner: %d\n",
file_stats.st_uid);
printf(" group ID of owner: %d\n",
file_stats.st_gid);
printf(" device type (if inode device): %lld\n",
file_stats.st_rdev);
printf(" total size, in bytes: %ld\n",
file_stats.st_size);
printf(" blocksize for filesystem I/O: %ld\n",
file_stats.st_blksize);
printf(" inode number: %lu\n",
file_stats.st_ino);
printf(" time of last access: %ld : %s",
file_stats.st_atime,
ctime(&file_stats.st_atime));
printf(" time of last change: %ld : %s",
file_stats.st_ctime,
ctime(&file_stats.st_ctime));
closedir(dp);
}

return EXIT_SUCCESS;
}

我想我需要将结构移动到 while 循环中,但是当我这样做时,编译器会提示“file_stats undeclared”。

最佳答案

除了 Valentin 的回答之外,您还应该将 closedir() 移出循环。

更新:您还需要将 stat(argv[1], ...) 替换为 stat(ep->d_name, ...) 获取有关实际文件的信息。但在此之前,您需要进入 argv[1] 目录(使用 chdir() 系统调用)。

完整的工作代码:

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

int main(int argc, char *argv[])
{
DIR *dp;
struct stat file_stats;
struct dirent *ep;
int ret = EXIT_SUCCESS;
int ret2;

if (argc != 2) {
fprintf(stderr, "Usage: %s FILE...\n", argv[0]);
return EXIT_FAILURE;
}

dp = opendir(argv[1]);
if (dp == NULL) {
perror("Couldn't open directory");
return EXIT_FAILURE;
}

ret2 = chdir(argv[1]);
if (ret2 == -1) {
perror("Unable to change directory");
ret = EXIT_FAILURE;
goto out1;
}

while ((ep = readdir(dp))) {
printf("filename: %s\n", ep->d_name);

if ((stat(ep->d_name, &file_stats)) == -1) {
perror("fstat");
ret = EXIT_FAILURE;
goto out2;
}

printf(" device: %lld\n",
file_stats.st_dev);
printf(" protection: %o\n",
file_stats.st_mode);
printf(" number of hard links: %d\n",
file_stats.st_nlink);
printf(" user ID of owner: %d\n",
file_stats.st_uid);
printf(" group ID of owner: %d\n",
file_stats.st_gid);
printf(" device type (if inode device): %lld\n",
file_stats.st_rdev);
printf(" total size, in bytes: %ld\n",
file_stats.st_size);
printf(" blocksize for filesystem I/O: %ld\n",
file_stats.st_blksize);
printf(" inode number: %lu\n",
file_stats.st_ino);
printf(" time of last access: %ld : %s",
file_stats.st_atime,
ctime(&file_stats.st_atime));
printf(" time of last change: %ld : %s\n",
file_stats.st_ctime,
ctime(&file_stats.st_ctime));
}

out2:
ret2 = chdir("..");
if (ret2 == -1) {
perror("Unable to change directory back");
ret = EXIT_FAILURE;
goto out1;
}

out1:
closedir(dp);
return ret;
}

关于c - 循环遍历文件并在 C 中打印文件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29548013/

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