7 Years
Discussion Span
Last Post by skp888

help me in listing file names inside directory by using a simple code in c++ that a beginner can understand

i believe that enumerating files/directories is OS dependent. so assuming your on windows, here are a few code snippets that will help you find files in a directory. the functions are defined in <windows.h>

WIN32_FIND_DATA fileData;
HANDLE hFind = FindFirstFile("C:\WINDOWS\*.exe", &fileData);

this will get the first .exe file that it can find in C:\WINDOWS, and fill the file's information into fileData

FindNextFile(hFind, &fileData)

calling this function will then get the next file matching that pattern. FindNextFile(...) will return NULL when it cant find anymore files.

note that subdirectories are sorta considered files, so will also be returned if they match the pattern (eg you use "C:\WINDOWS\*"), so you can check what is a directory (ie not a file) with:

fileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY

so, using these functions in a simple do-while loop will cycle through every file or subdirectory in a particular folder

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.