Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8307737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:35:50+00:00 2026-06-08T18:35:50+00:00

I wish to read all the text files in a particular folder. The files’

  • 0

I wish to read all the text files in a particular folder. The files’ names do not have any common pattern in them- else the task would have been easier.

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

It will be good if I could work around with sub-folders as well, but even the basic implementation is sufficient.

I tried looking at the previously asked related questions (here, here, here and here), but none of them give a C and Linux specific answer which I need.

edit : So, this is what I wrote based on the answers received-

#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;
     }

And the error received-
pnp@pnp-laptop:~/snort_rules_folder$ ./a.out
Error : Failed to open entry file

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:35:51+00:00Added an answer on June 8, 2026 at 6:35 pm

    You can use this sample code and modify it if you need:

    #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;
    }
    

    Hope this hellp.

    Regards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several XML files that I wish to read attributes from. My main
Good afternoon, I wish to have a script that will look for all files
I have a binary file I wish to read for my android app. the
I wish to process files (.krn-files that can be read as txtfiles) and replace
I want to read all thumbnails from a folder with images in Windows XP.
I have a text file which I read using readlines(). I need to start
I wish to edit ini files over web server, decided to use django, been
I have several gigabytes of documents which I wish to store online somewhere in
I am loading forms from a file using slurp, read, and cons'ing them together
After years of programming, we all have a set of small functions used as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.