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 6812235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:25:28+00:00 2026-05-26T20:25:28+00:00

I’m trying to create a function that scans a folder on my Windows PC

  • 0

I’m trying to create a function that scans a folder on my Windows PC and every time it does, a file called “Filter.txt” is appended with the string “Test Script”.

Now the problems are 2, the first is that the scan must be performed either in the directory c:\LOG or its subdirectories, and the second is that I do not know how to chain fopen in the directory and the name of the file.

int main(){
    DIR *dir;
    FILE * pFile;
    char myString[100];
    struct dirent *ent;
    dir = opendir ("c:\\LOG");
    if (dir != NULL) {
        /* print all the files and directories */
        while ((ent = readdir (dir)) != NULL) {
            pFile = fopen ("Filter.txt","a");
            if (pFile==NULL)
                perror("Error");  
            else
                fprintf(pFile,"%s\n","Test scriptIno");
            fclose(pFile);
            //printf ("%s\n", ent->d_name);
        }
        closedir (dir);
    } else {
        /* Can not open directory */
        perror ("");
        return EXIT_FAILURE;
    }
}
  • 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-05-26T20:25:29+00:00Added an answer on May 26, 2026 at 8:25 pm

    For how to chain calls to opendir you can find plenty of answers here on SO, for example this. Use ent->d_type to check if the entry is a directory or a file.

    For opening a file in the directory, just use the pathname in ent->d_name to construct the path for the fopen call.

    Edit Was a little bored at work, and made a function like the one you maybe want…

    #ifdef _WIN32
    # define DIR_SEPARATOR "\\"
    #else
    # define DIR_SEPARATOR "/"
    #endif
    void my_readdir(const char *path)
    {
        DIR *dir = opendir(path);
        if (dir != NULL)
        {
            struct dirent *ent;
    
            static const char filtername[] = "filter.txt";
    
            /* +2: One for directory separator, one for string terminator */
            char *filename = (char *) malloc(strlen(path) + strlen(filtername) + 2);
    
            strcpy(filename, path);
            strcat(filename, DIR_SEPARATOR);
            strcat(filename, filtername);
    
            FILE *fp = fopen(filename, "a");
    
            while ((ent = readdir(dir)) != NULL)
            {
                if (ent->d_type == DT_REG || ent->d_type == DT_DIR)
                {
                    if (strcmp(ent->d_name, "..") != 0 && strcmp(ent->d_name, ".") != 0)
                    {
                        if (fp != NULL)
                            fprintf(fp, "%s : %s\n", (ent->d_type == DT_REG ? "File" : "Directory"), ent->d_name);
    
                        if (ent->d_type == DT_DIR)
                        {
                            /* +2: One for directory separator, one for string terminator */
                            char *newpath = (char *) malloc(strlen(path) + strlen(ent->d_name) + 2);
    
                            strcpy(newpath, path);
                            strcat(newpath, DIR_SEPARATOR);
                            strcat(newpath, ent->d_name);
    
                            /* Call myself recusively */
                            my_readdir(newpath);
    
                            free(newpath);
                        }
                    }
                }
            }
    
            if (fp != NULL)
                fclose(fp);
            free(filename);
        }
    }
    

    Edit It seems that the opendir and readdir functions are not very well supported on Windows. Use the Windows-only FindFirstFile and FindNextFile in a similar fashion to my example above. See this MSDN page for an example on how to use these functions.

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace

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.