【问题标题】:How to read all files in a folder using C如何使用C读取文件夹中的所有文件
【发布时间】:2021-04-29 17:38:53
【问题描述】:

我希望阅读特定文件夹中的所有文本文件。这些文件的名称没有任何共同的模式 - 否则任务会更容易。

//read a file from the directory  
//Perform a common operation  
//write output to a common file  
//read the next file

如果我也可以处理子文件夹会很好,但即使是基本实现也足够了。

我尝试查看之前提出的相关问题(herehereherehere),但没有一个给出我需要的 C 和 Linux 特定答案。

edit:所以,这是我根据收到的答案写的-

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


int main(int argc, char **argv)
{
    DIR* FD;
    struct dirent* in_file;
    FILE    *output_file;
    FILE    *entry_file;
    char    buffer[BUFSIZ];

    /* Opening common file for writing */
    output_file = fopen("/home/pnp/snort_rules_folder/rulesoutput.txt", "a+");
    if (output_file == NULL)
    {
        fprintf(stderr, "Error : Failed to open output_file\n");

        return 1;
    }

    /* Scanning the in directory */
    if (NULL == (FD = opendir ("/home/pnp/snort_rules_folder/rules"))) 
    {
        fprintf(stderr, "Error : Failed to open input directory\n");
        fclose(output_file);

        return 1;
    }
    while ((in_file = readdir(FD))) 
    {
        /* On linux/Unix we don't want current and parent directories
         * If you're on Windows machine remove this two lines
         */
        if (!strcmp (in_file->d_name, "."))
            continue;
        if (!strcmp (in_file->d_name, ".."))    
            continue;
        /* Open directory entry file for common operation */
        /* TODO : change permissions to meet your need! */
        entry_file = fopen(in_file->d_name, "r");
        if (entry_file == NULL)
        {
            fprintf(stderr, "Error : Failed to open entry file\n");
            fclose(output_file);

            return 1;
        }

        /* Doing some stuff with entry_file : */

        while (fgets(buffer, BUFSIZ, entry_file) != NULL)
        {
            /* Use fprintf or fwrite to write some stuff into common_file*/
        }

    fprintf(output_file, "reading file %s", in_file->d_name);

        /* When you finish with the file, close it */
        fclose(entry_file);
    }

    /* Don't forget to close common file before leaving */
    fclose(output_file);

    return 0;
     }

并且收到错误-
pnp@pnp-laptop:~/snort_rules_folder$ ./a.out
错误:无法打开入口文件

【问题讨论】:

  • 我认为您无法找到重复项的原因是使用了“文件夹”一词而不是更常见的“目录”(至少在 Linux 平台上)。
  • 你能用find -type f -exec your_program {} + &gt;output.file吗?
  • @GregHewgill 嗯...你提到的这个问题不涉及“文件名”...我说的是阅读文件...
  • 好吧,一旦你有了文件名,那么打开文件并阅读它们就非常简单了。 (或者这就是你的问题?)

标签: c linux file io directory


【解决方案1】:

您可以使用此示例代码并根据需要对其进行修改:

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

/* This is just a sample code, modify it to meet your need */
int main(int argc, char **argv)
{
    DIR* FD;
    struct dirent* in_file;
    FILE    *common_file;
    FILE    *entry_file;
    char    buffer[BUFSIZ];

    /* Openiing common file for writing */
    common_file = fopen(path_to_your_common_file, "w");
    if (common_file == NULL)
    {
        fprintf(stderr, "Error : Failed to open common_file - %s\n", strerror(errno));

        return 1;
    }

    /* Scanning the in directory */
    if (NULL == (FD = opendir (in_dir))) 
    {
        fprintf(stderr, "Error : Failed to open input directory - %s\n", strerror(errno));
        fclose(common_file);

        return 1;
    }
    while ((in_file = readdir(FD))) 
    {
        /* On linux/Unix we don't want current and parent directories
         * On windows machine too, thanks Greg Hewgill
         */
        if (!strcmp (in_file->d_name, "."))
            continue;
        if (!strcmp (in_file->d_name, ".."))    
            continue;
        /* Open directory entry file for common operation */
        /* TODO : change permissions to meet your need! */
        entry_file = fopen(in_file->d_name, "rw");
        if (entry_file == NULL)
        {
            fprintf(stderr, "Error : Failed to open entry file - %s\n", strerror(errno));
            fclose(common_file);

            return 1;
        }

        /* Doing some struf with entry_file : */
        /* For example use fgets */
        while (fgets(buffer, BUFSIZ, entry_file) != NULL)
        {
            /* Use fprintf or fwrite to write some stuff into common_file*/
        }

        /* When you finish with the file, close it */
        fclose(entry_file);
    }

    /* Don't forget to close common file before leaving */
    fclose(common_file);

    return 0;
}

希望这能帮上忙。

问候。

【讨论】:

  • 谢谢。如果您不介意更多帮助,这是我修改后的代码-pastebin.com/4jFTHY6Z,我收到的错误写在最后。
  • @pnp :您可以在此处编辑帖子并发布代码,以获得更多帮助。
  • @pnp,pl。检查您是否从文件所在的同一目录运行代码。如果您从其他目录运行代码,则 fopen 可能没有获取文件的完整路径。你也可以添加一个错误报告库函数,比如 perror 而不是你的错误处理。这会给你错误的确切内容。现在,如果您正在阅读的目录包含其他目录(而不是文件),您是否希望 fopen 也适用于这些目录?似乎没有。所以你需要添加一个检查以继续循环,以防你得到一个目录。(stat函数可以帮助)
  • 正如@Tanmoy 所说,提供文件的完整路径,当然,还要将 errno 添加到您的错误中以查看发生了什么,我将编辑我的代码以添加它。
  • @TOC 感谢 TOC 添加 errno 报告。还会询问您是否可以添加代码来统计文件名,如果这是一个目录,请继续循环。这应该会使答案更好。
猜你喜欢
  • 1970-01-01
  • 2011-02-24
  • 1970-01-01
  • 1970-01-01
  • 2021-01-08
  • 2013-01-15
  • 2011-08-15
  • 2010-12-23
相关资源
最近更新 更多