Sign in to follow this  

list maps (from harddrive folder)

This topic is 4092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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
}


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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