Is there a c++ function that allows you to show all the txt files in a folder? I'm using the Borland C++ Compiler V4.52.
Printable View
Is there a c++ function that allows you to show all the txt files in a folder? I'm using the Borland C++ Compiler V4.52.
You can use the WinAPI functions FindFirst, FindNext and FindClose
Here's a sample function I wrote:
Code:bool outputDirectory(ostream &outputFile, const char *pcszPath)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(pcszPath, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return 1;
} else {
do {
outputFile << FindFileData.cFileName << endl;
} while (FindNextFile(hFind, &FindFileData));
}
FindClose(hFind);
return 0;
}
I suppose I should show how to use the function...
Code:#include <windows.h>
#include <fstream.h>
bool outputDirectory(ostream &outputFile, const char *pcszPath)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(pcszPath, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return 1;
} else {
do {
outputFile << FindFileData.cFileName << endl;
} while (FindNextFile(hFind, &FindFileData));
}
FindClose(hFind);
return 0;
}
int main (int argc, char *argv[])
{
ofstream fout("c:\\list.txt", ios::trunc);
outputDirectory(fout, "c:\\*.txt"); // This will print out all files with the extension txt.
cout << "Done" << endl;
fout.close();
return 0;
}