and about 3 million of those? I just wish to know, because I am after a Terrain Mesh editor, and i want a way to save it, but not as an image file.
I just want to know if it would take a long time to read them, and wether it is efficient or not? Also, if you find this way inefficient, would you please care to propose a more efficient way mabe? Thank you very much.
ok the code may be wrong but you have the usefull fonctions.
fopen
fscanf
malloc /* may not be neccessary if you have a static array */
fclose
the use of an “addlog” is interesting …
you keep a text file in which you write debug informations…
you can even exit the program if there is an error after having written the reason and freed memory.
int mapH;
int mapW;
char mappath[1024];
int map_load()
{
int i,j;
FILE *F;
// we open the file mappath (string)
if(!(F = fopen(mappath,"r"))) addlog("Unable To open map File");
// the first two int of your file should be the size of your map
// do whatever you want but this is helpfull ^^
if(fscanf (F, "%d %d
", &mapH, &mapW) != 2) addlog("Invalid map file");
// memory allocaton for an array (mapH*mapW of int)
//MAP is an array of array of int ^^ int * * MAP
if(!(MAP = (int)malloc(mapH*sizeof(int)))) addlog("error malloc1");
for(j=0;j<mapH;j++)
if(!(MAP[j] = (int)malloc(mapW*sizeof(int)))) addlog("error malloc2");
for (j=0;j<mapH;j++)
for(i=0;i<mapW;i++)
{
if(fscanf (F,"%d
", &MAP[j][i])!=1) addlog("error during map loading");
}
fclose(F);
return(0);
}
don’t have time to test it but it works in our project (but i have modified a little the code).
you should have no problems to WRITE the map file.
hey, thanq very much. I c, so u would WRITE a binary file same way as a text file? I c that this was a lot of writing off the top of ur head, but mabe LATER, if u find some time, could u give me an explanation on how to read a binary?
HAHA, sorry, i just left to have some food, and while eating crackers, i realized what i just wrote. haha, sorry, its my pure stupidity. haha, well, i apologise.
Regards,
Tim
EDIT:
so i wouldnt have to double post. What do u mean in this line:
but what if i have about 2,999,998 other numbers there? I mean, arent binaries same thing as unformated text files? I mean, how would it know that THOSE are the right numbers?
OH, im just stupid. So i have to specify the width and height during the declaration for instance, and if both match with the ones of the file, then the file is the one we are looking for. I c, thanq. I didnt realize that at first for some reason.
well if you have 3x10^6 int
your file should contains 3x10^6 int + 2.
(don’t forget it is for an heightmap and and it will probably be square or rectangular.)
with the first fscanf you get the width and the height.
with these sizes you can allocate your memory
and do a double loop to parse your array and fill it with the values returned by fscanf.
this subject is a little bit of topic … i only see an indirect link to OpenGL, but i am glad to help.
if you have more problems i suggest using IRC.
you can find me on quakenet #brucemangy