Getting file-names in a directory

Started by
2 comments, last by Evil Steve 18 years, 1 month ago
Hi! I'm searching for a method/algorithm or a way how to get every filename in a specified folder. I used the search function, but the only thing I found was "outdatet" because dirent.h doesnt exists (anymore?). Also searched articles on gamedev but found nothing that could help me. At the moment I'm using ms visual c++ 2005. thanks in advance
Advertisement
Here is a relevant topic:

File Searching
A short question:
My source looks like this:
BOOL xyz::GetFilesFromDirectory(char* Path){	string searchPath = Path;	HANDLE fileSearcher;	BOOL worked = FALSE;	LPWIN32_FIND_DATA DataInformation = NULL;	if(!m_DirectorySearched){		fileSearcher = FindFirstFile(Path, DataInformation);		if(fileSearcher != INVALID_HANDLE_VALUE && DataInformation != NULL){			worked = TRUE;			m_FileNames.push_back(DataInformation->cFileName);			while(FindNextFile(fileSearcher, DataInformation)){				m_FileNames.push_back(DataInformation->cFileName);			}		}	}	return worked;}


The path is:
"Data\\Maps"

The problem is that he doesnt find any file.
In the documentaition is written that you can use wildcars for files and some other stuff.
Tried with the path:
"Data\\Maps\\*.dat"
But if I use this path I always get an unhandled exception.

Any suggestions?
You need to allocate your DataInformation variable, not pass a NULL pointer.
I.e.
HANDLE hFind;WIN32_FIND_DATA theFind;hFind = FindFirstFile(Path,&theFind);if(hFind == INVALID_HANDLE_VALUE)   return false;do {   // Add filename to list} while(FindNextFile(hFind,&theFind);FindClose(hFind);   // Don't forget this!


Edit:
You want to pass "Data\\Maps\\*.dat" as your path. If you pass "Data\\Maps", then there won't be any files that match that path, so FindFirstFile() returns INVALID_HANDLE_VALUE, and DataInformation is never filled. When you pass "Data\\Maps\\*.dat", DataInformation gets used, and you get an access violation as Win32 tries to write to a NULL pointer.

[Edited by - Evil Steve on March 5, 2006 1:00:55 PM]

This topic is closed to new replies.

Advertisement