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

Dani AI

Generated

This thread already has a usable Windows example from and confirmation from . For a production-ready approach, treat the task as three small problems: (1) enumerate entries in the source folder, (2) decide whether each entry is a file or a directory (and whether to recurse), and (3) build a safe destination path, open the input, process it, and write the output. Keep resource use low: do not keep many files open at once, handle errors on each I/O operation, and create any needed output directories before writing.

Practical tips and common pitfalls: skip the special entries "." and ".."; avoid buffer overflows when joining paths (use safe join helpers or length-checked functions); on Windows be aware of Unicode and long-path issues (use wide-character APIs or the long-path prefix when necessary); detect and avoid following symbolic links or reparse points to prevent infinite recursion; preserve relative paths under c:\output if folder hierarchy should be retained; handle filename collisions (overwrite, rename, or fail explicitly); close handles promptly; report errors with meaningful messages (errno or GetLastError). For very large numbers of files, favor streaming reads/writes and consider an explicit queue or worker threads — but protect shared resources and watch for file locking issues.

A minimal recursive workflow in pseudocode:

processDir(inDir, outDir):
    create outDir if missing
    for entry in list_directory(inDir):
        if entry is "." or "..": continue
        inPath = join(inDir, entry)
        outPath = join(outDir, entry)
        if is_directory(inPath):
            processDir(inPath, outPath)
        else:
            open inPath, read/process, write outPath

For nested-folder questions (see ), recursion like the pseudocode above is simplest; an explicit stack can replace recursion if very deep trees are expected (to avoid stack overflow), as suggested.

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.