Skip to main content
GameDev.net gamedev.net
🔒 Locked

Getting List Of Files In A Folder? (C++, Win32) [Question Update*]

Started by ajm113 Feb 3, 2009 at 11:04 PM 6 replies 6.7k views
Original Post
ajm113
ajm113
Hi, I want to get a list of file names into a vector or a list perhaps. So a list of maps/file with a specific format will get stored in the list? I want to show a file list of music files the user can play, but I don't know what would be the best case on this. I was thinking on FindNextFile or something like that, but I was wondering if there was a alternative? Thanks, Andrew. [NEW Question Is Asked Below] [Edited by - ajm113 on February 5, 2009 4:28:53 PM]
Check out my open source code projects/libraries! My Homepage You may learn something.
Zakwayda
Zakwayda
If you're interested in a cross-platform solution, check out the Boost Filesystem library.
ajm113
ajm113
mmm, I rather not. I want to be basic and not use any library's that would suck up memory and time for 90% of functions I'm not even going to bother using with the game.
Check out my open source code projects/libraries! My Homepage You may learn something.
Zakwayda
Zakwayda
Quote:
Original post by ajm113
mmm, I rather not. I want to be basic and not use any library's that would suck up memory and time for 90% of functions I'm not even going to bother using with the game.
I think it's likely your concerns about using Boost are unfounded, but anyway, try searching the archives for 'files in folder'. (I just tried it, and it looks like the third hit returned includes some info that might be useful to you.)
Anon Mike
Anon Mike
It's not at all clear what exactly it is that you want.

If you want a dialog that allows users to browse and select files then the standard one is GetOpenFileName.

If you want a list of files that you will process and/or display in your own way then FindFirst/FindNextFile is the way to go. Since you don't say why those are unacceptable to you I can't give you an alternative.
-Mike
ajm113
ajm113
Yeah ok, that FindNextFile works fine for me.

Just loosing data of a vector when I try to save the file names into it though. The vector and function its running on are in the same class, but when it seems when that function where the vector is handled at is done executing and moves on into a different function and calls a function not apart of that class it losses the data.


//InitFunction WIN32_FIND_DATA findFileData;    HANDLE hFind = FindFirstFile("resources\\Music\\*.mp3", &findFileData);   	if(hFind  == INVALID_HANDLE_VALUE) {		m_pErrorHandler.Message("Could Not Find Any Music!");    }	  SongsLoaded.push_back(findFileData.cFileName);    while(FindNextFile(hFind, &findFileData)) {	SongsLoaded.push_back(findFileData.cFileName);    }     FindClose(hFind);	char Dest[565];	char* SongFile = SongsLoaded.at(0);	sprintf(Dest, "resources\/Music\/%s", SongFile);				 //Load Music//End of function
Check out my open source code projects/libraries! My Homepage You may learn something.
rip-off
rip-off
The pointers returned by FindNextFile are only valid as long as the HANDLE is valid. You need to store copies of the contents of these strings. I suggest using std::vector.
ajm113
ajm113
Yeah that seems to work!

The only problem now is that I have two files in the folder and for some reason its not adding the second file, but it can find it. It also looks that it adds extra blank/null elements in the vector two. Is there something else about the snippet too making this bug?
Check out my open source code projects/libraries! My Homepage You may learn something.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.