Advertisement

How can I get the files in a directory?

Started by November 30, 2000 10:24 AM
0 comments, last by wolfman8k 23 years, 11 months ago
I have a game that saves files to a directory, and I need to be able to open them, but I don''t know the names. I need something like this:
  

changedir("C:\Mygame");
num_of_files = get_file_count();
char filename[8][num_of_files];

for(i=0; i<num_of_files; i++)
{
filename<i>=name_of_file(i);
}

  
Thanks
This code will stick the names of all the files in the current directory (and recursively in subdirectories) into s_vecFilenames. (the files in subdirectories will have their full paths, like "Textures\Foo.tga") It skips over "." and ".." obviously (actually, any filename that begins with ".").

  #include <windows.h>#include <vector>#include <string>using namespace std;void main(){   EnumerateAllFiles("");}static vector<string> s_vecFilenames;void EnumerateAllFiles(const char* szDir){	WIN32_FIND_DATA wfd;	memset(&wfd, 0, sizeof(WIN32_FIND_DATA));	char szPath[1024];	sprintf(szPath, "%s*.*", szDir);	HANDLE hFile = FindFirstFile(szPath, &wfd);	if ( INVALID_HANDLE_VALUE != hFile )	{		do		{			if ( wfd.cFileName[0] == ''.'' )			{			}			else if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )			{				char szDirChild[1024];				sprintf(szDirChild, "%s%s\\", szDir, wfd.cFileName);				EnumerateAllFiles(szDirChild);			}			else			{				char szFilename[1024];				sprintf(szFilename, "%s%s", szDir, wfd.cFileName);				s_vecFilenames.push_back(szFilename);			}		}		while ( FindNextFile(hFile, &wfd) );	}}  

This topic is closed to new replies.

Advertisement