I want to read all files in a particular folder using C programming. HOw to do this. Thanks.

Ex: I have some 5 files in folder named c:\temp.
I want to read all files one after another and want to write output file for each input file.
The output files has to be saved in another folder named c:\output

How to do this. Plz any one give me suggestion how to start

Recommended Answers

All 4 Replies

No standard functions to scan directories.
On Windows use FindFirstFile/FindNextFile API, for example:

#include <windows.h>
#include <string.h>

void DirScanStub()
{
    const char dirName[] = "c:\\temp\\";
    const char dirScan[] = "c:\\temp\\*.*";
    char* pname = 0; /* File name buffer */
    WIN32_FIND_DATA info;
    HANDLE h = FindFirstFile(dirScan,&info);
    if (h == INVALID_HANDLE_VALUE)
    {
        /* print message?.. */
        return;
    }
    pname = (char*)malloc(MAX_PATH); /* (char*) for C++ only */
    do
    {
        if ((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
            continue; /* skip directories */
        strcat(strcpy(pname,dirName),info.cFileName);
        /* Now we have the next file name...    */
        /* Open, process then close the file ...*/
    } while (FindNextFile(h,&info));
    FindClose(h); /* Close dir scan */
    free(pname);
}

Thanks its working fine

hi,
using this methodology, is there a piece of code I could write if after where a folder in directory is detected to scan all files in that folder and then return to the root folder to continue?

Many thanks

commented: Don't resurrect dead threads. -2

hi,
using this methodology, is there a piece of code I could write if after where a folder in directory is detected to scan all files in that folder and then return to the root folder to continue?

Many thanks

Use recursion. There are some examples of that in the code snippets.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.