How to read files in a folder
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
ambarisha.kn
Junior Poster in Training
66 posts since Jun 2008
Reputation Points: 25
Solved Threads: 0
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);
}
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
ambarisha.kn
Junior Poster in Training
66 posts since Jun 2008
Reputation Points: 25
Solved Threads: 0
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343