list maps (from harddrive folder)

Started by
8 comments, last by suliman 17 years, 6 months ago
Hi I have a mapfolder and must be able from my game to list all available *.map files. And i dont really know how to achieve this... Thanks Erik
Advertisement
What language?
What operating system?

Directory manipulation is typically a platform-dependent operation.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Look up boost::filesystem. There's no *standard* way to do this because C++ doesn't know you have a map folder - and you can't tell it you do - because it doesn't know what a folder is. (This is so that C++ compilers can exist for hardware that doesn't normally even have an operating system installed on it.)
oh ok.

i use c++ on windows32. I can save and load maps using strings like "mymap.map" (i use fread fwrite for this purpose). I just dont know how to find files if i dont know the exact file name. (to have the player look in the folder and write the name himself isnt really optimal but the only thing i know at the moment.

Erik
FindFirstFile and FindNextFile are the Win32 API calls you are looking for.

Alternatively your compiler will possibly have a non-standard header, commonly called something like <dir.h> that will offer some compiler-dependant calls for this purpose, but they probably just wrap the calls above.

Also, I have to ask, if you are using C++, why are you using fwrite and fread rather than std::fstreams?
hmm its just my habit. i havnt checked that one up. Is it easier to use?
(i use ms visual c++ to compile)
Not just easier - safer.

#include <cstdio>#include <fstream>using namespace std;bool cstyle(){    char buf[32];    FILE *fp=fopen("test.txt","rb"); if(!fp) return false;    fread(buf,sizeof(char),32,fp);    CallAFunctionThatThrowsException(); // oops - file not flushed and closed    if(TestData(buf)==false) return false; // oops - file not flushed and closed    fclose(fp); return true;}bool cppstyle(){    char buf[32];    ifstream is("test.txt",ios::binary); if(!is.is_open()) return false;    is.read(buf,32);    CallAFunctionThatThrowsException(); // no problem - file closed properly    if(TestData(buf)==false) return false; // no problem - file closed properly    return true;}


The only slight complication, if that is the word, is that fstream::read expects a char* pointer rather than a void* pointer for the buffer it is writing to, so if you are trying to read into an int for example, you have to do

int i;is.read((char*)&i,sizeof(int));


The other chief advantage of using streams is when you are not reading or writing binary data, but text. In the same way that fprintf works the same as printf, a text ofstream works the same way as cout.

Unlike the C stuff though, since they inherit from the same base you can do stuff like this:

#include <iostream>#include <fstream>#include <string>using namespace std;void out(ostream &os,const string &text){    os << text << endl;}int main(){    out(cout,"hello"); // prints to screen    ofstream os("dump.txt");    out(os,"hello"); // same function now prints to file}
HANDLE hFind;
1) hFind= FindFirstFile("data/maps/*.map",0);
2) hFind= FindFirstFile("data/maps/",0);
3) hFind= FindFirstFile("data/maps/1.map",0);

1)access violation crash
2)no file found
3)access violation crash (the specified file exists)

What am i doing wrong?
Thanks
Erik
You can't pass 0 as the second argument. You have to pass the address of a WIN32_FIND_DATA structure that recieves information about the file:

void f(){    WIN32_FIND_DATA Data;    HANDLE hFind;    hFind = FindFirstFile("*.txt", &Data);    if(hFind!=INVALID_HANDLE_VALUE)         {        cout << Data.cFileName << endl; // found file name        FindClose(hFind);        }}


If you want to keep searching, you then call bool FindNextFile(hFind,&Data) which returns false when it can't find any more files.

The link I provided above goes into a lot more detail about these functions that I have.
thanks it works now!

this really steps my app up a bit from totally temporary sollution.
Erik

This topic is closed to new replies.

Advertisement