Sign in to follow this  
Mizipzor

Detect if file exists

Recommended Posts

Mizipzor    247
Up until now Ive always done "Input map to load: ", read what user wrote, open the file and load the data. But I want to do: "Which map do you want to play? [1] map1 [2] map2" and so on... The program lists all maps in the maps folder then the player just clicks with the mouse or presses one button and the map loads. Is there anyway to check for (txt) files in this way? To list all existing in a directory?

Share this post


Link to post
Share on other sites
Mizipzor    247
Quote:
Original post by AAAP
FILE * file = fopen(filename, "rt");

if(file == null){
file not found
}


i think this would work, not 100% positive.


But that way I would need to manually add all the files thats should be listed? I cannot just let the user place a new map in the mapfolder and have it appear in the maplist ingame?

Share this post


Link to post
Share on other sites
AAAP    137
ok i see. I have a bad habit of not completely reading peoples threads eh.

I believe, code to list files in a directory is platform specific, because it would have to access the filesystem through the operating system

Share this post


Link to post
Share on other sites
Mizipzor    247
Those FindFirstFile() functions seems good, but can I put relative paths in there? Tried with "\\Maps\\" but it didnt work, works with full path though: "C:\\Project\\Maps".

Share this post


Link to post
Share on other sites
Zao    971
If you're using boost, you could use boost::filesystem's directory_iterator to retrieve all the entries in a directory.

The .leaf of a path is the rightmost part of a path delimited by '/', in this case the filename.

A directory_iterator starts on the first entry of the path and directory_iterator() is equivalent to the entry past the end of the directory.

The following code iterates over all the files in a folder and does something to them.

And yeah, it's portable to any platform the boost library works on, which is virtually all of them.

for(boost::filesystem::directory_iterator I("./Maps/"); I != boost::filesystem::directory_iterator(); ++I)
{
if(!boost::filesystem::is_directory(*I))
{
DoStuffWith(I->leaf()); // leaf() returns a path, which has implicit converters to and from std::string.
}
}



The boost doc on ::filesystem

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this