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

Recommended Answers

All 2 Replies

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

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.