c - 在C中,有没有办法获取名称与字符串匹配的多个文件的大小?

标签 c

我有这样的文件:

  • file1_a_etc.txt,
  • file1_b_etc.txt
  • file2_a_z.txt
  • file2_b_z.txt

我想获取带有“a”的文件的大小,即 file2_a_z.txt 和 file1_a_etc.txt

我通过这种方式获得了大量文件,因此无法单独指定每个名称。

我是 C 语言的初学者。
我知道如何读取单个文件的大小。我正在 window 上工作。

#include <stdio.h>       
#include <sys/stat.h>   // For struct stat and stat()

struct stat attr;
void main()
{   
if(stat("filename.txt", &attr) == 0)
{

    float x;
    x=(attr.st_size)/1048576.0;             //1MB=1048576 bytes
    printf("Filesize: %.2f MB", x);
}
else
{
  // couldn't open the file
    printf("Couldn't get file attributes...");
}
}

最佳答案

对于 Windows 控制台,有函数 _findfirst。对于第一个参数,输入 *a*.txt

关于c - 在C中,有没有办法获取名称与字符串匹配的多个文件的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28601199/

相关文章:

c - 使用opencv进行视频采集

C 程序无法运行

c - 在c程序中执行SET命令

c - 在 glibc 上覆盖 pthread 函数但在 musl 上不覆盖时出现神秘的段错误

检查多边形是否凸

java - 从 C 程序编写十六进制字节代码 Java

c++ - 分解在 Visual C++ 中不起作用的函数

c++ - MPI_Scatterv 仅分散自定义 MPI_Datatype 的一部分

c - C中的函数内函数

c - 如果#define 替换 errno 符号,线程安全的 errno 是如何初始化的?