Skip to main content
gamedev.net
🔒 Locked

Directory Listing

Started by Stealth2000 2 replies 1k views
Original Post
Stealth2000
Stealth2000
I''ve been looking for the solution to this question for a while now, but I can''t find a reference to it in my C++ book or anywhere on the internet. I need to get a list of the files in a directory, and then randomly choose one. But how do you get a directory listing in C++? I can''t find any way to do it. Help please. Thanks
cybrgeek
cybrgeek
I don''t have a compiler at hand, so this may not compile. This will get a list of files in a directory, as far as picking a random one there is more than one way to do that. I would copy the results to a list, and just pick a random one.


WIN32_FIND_DATA fdFind;
HANDLE hFind;
bool bContinue;

hFind = ::FindFirstFile("c:\\somedir\\*.*", &fdFind);

bContinue = (hFind != INVALID_HANDLE_VALUE);

while(bContinue)
{
//This will just output the file names to the output window.
::OutputDebugString(fdFind.cFileName);
::OutputDebugString("\n");

bContinue = (bool)::FindNextFile(hFind, &fdFind);
}

if(hFind != INVALID_HANDLE_VALUE)
::FindClose(hFind);
Stealth2000
Stealth2000
Thanks cybrgeek... that code works perfectly. Just a small adjustment needed to work with my logging functions to check the output.

I''m sure I can work with this to do what I need now.

Topic Locked

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

Sign in to reply to this topic.