How to list all the .txt files in a folder?
I'm currently working on a level editor in ascii, which is coming along good. But I've ran into a rather large problem. This is my problem: When the user clicks on the console where it says "Load Level", I have a menu pop up that will list all of the level .txt documents in the Level Editor folder. I was wondering how would I be able to generate a list of all the .txt files in a folder?
For example: Say we have a folder named "Level Editor" with levelA.txt and levelB.txt and levelC.txt and the program doesn't know how many files are in the folder. For all it knows their could be 1 or 20 .txt files in the "Level Editor" folder. How would the program be able to provide a list of all the .txt files in the folder to the user/player?
So on the screen it would show the user and the user would click on the file name. This part I can do, I just can't get the program to list all the .txt files in the folder.
[rough draft idea]
Please click on the level you would like to load
------------------------------------------------
levelA.txt
levelB.txt
levelC.txt
[eof rough draft idea]
Thank you ahead of time for all/any help!
Which system? On Windows you can use FindFirstFile() and FindNextFile() -- see MSDN for how they work. On cross-platform I think the boost library will supply such things, but then again I don't know anything about boost so I'll pass on that one.
Greetz,
Illco
PS: made a link to MSDN.
Greetz,
Illco
PS: made a link to MSDN.
Its on Windows, and I've tried looking in to using _finddata_t and _findfirst() but I didn't have to much luck... = / Maybe I should give it a second look...?
Edit: I'm using C++ and the compiler if it matters is C++.NET
Edit: I'm using C++ and the compiler if it matters is C++.NET
I am assuming you're using Windows, but I could be totally wrong. In case you're using Windows, hang thight. In case you're not, you can either turn off the internets, or read along.
The following code is platform-specific for Windows:
Using this code:
This is some old code, so it MIGHT contain some errors, but you'll manage I hope. Otherwise you know where the find me(Hint: pm).
Toolmaker
The following code is platform-specific for Windows:
#ifndef _WIN32_FILESEARCH_H_#define _WIN32_FILESEARCH_H_#include <string>#include <windows.h>using std::string;class CFileSearch{public: CFileSearch():m_hHandle(INVALID_HANDLE_VALUE) {} CFileSearch(const string& strSearch) { Search(strSearch); } // Search // Does a search and stores the result. Will return false on error bool Search(const string& strSearch) { m_bHasNext = true; m_hHandle = FindFirstFile(strSearch.c_str(), &m_hSearchResult); if (m_hHandle == INVALID_HANDLE_VALUE) m_bHasNext = false; return m_bHasNext; } // GetNext // Places the next return value in strResult. Will return false when there is no // new result bool GetNext(string &Result) { if (!m_bHasNext) return (false); Result = m_hSearchResult.cFileName; m_bHasNext = FindNextFile(m_hHandle, &m_hSearchResult) ? true : false; return (true); }private: HANDLE m_hHandle; WIN32_FIND_DATA m_hSearchResult; bool m_bHasNext;};#endif
Using this code:
string path = "c:\\leveleditor\";string result;cout << "Current directory: " << path << endl;CFileSearch search(path + "*.txt");while (search.GetNext(result)){ cout << result << endl;}
This is some old code, so it MIGHT contain some errors, but you'll manage I hope. Otherwise you know where the find me(Hint: pm).
Toolmaker
Thank you everyone and thank you so much Toolmaker! The code still works and it works good! And it gave me a good reference to look at. Now I'm going to go and try to go code something that works like it, so I can learn how to do it and impleament it in my level editor. I also need to go do some research/reading on "FindFirstFile() and FindNextFile()".
Thank you so much again everyone!
Thank you so much again everyone!
The boost::filesystem library presents a much nicer interface than the win32 api or posix, and gives you portability to boot.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement