Reading Text Files

Hello everyone, how would an avarage computer go about reading about reading the y values of a heightmap from a text file? For instance like

1 34 65 76 234 675 23 65 12 76 32 12 65 12 65 87 12

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.

Regards,
Tim

hi,
if you use text files you will have to do an
ascii to int conversion.

you will lose space on your hard disk and time for the conversion.
Consider using binary file.
It is not really hard.


brucemangy

Im really a beginner. If you have time would u care to explain how to use binary files, please?

I really appreciate your help. Thanq.

Regards,
Tim

to help you i need to know what language you are
using.

I MAY be able to help you in delphi and C.


brucemangy

C++, using visual C++ compiler. Sorry, i keep thinking everyone uses the same language :slight_smile:

Can u help me?

Thanq

Regards,
Tim

EDIT: Visual C++ 6

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.

same principle.
File *F;
F=fopen(“filename”,“w”);
fprintf(%d%d,mapH,mapW);
// double loop
fprintf(%d,your_int);
fclose(F);

there may be some errors, but i gave you quite a exhaustive answer ^^


brucemangy

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?

Thanx a bunch!

Cheers,
Tim

the code i gave you is for READING…
(see the fscanf ^^)
and it’s pretty much the same for WRITING
(fprintf)
(see principle)

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:

if(fscanf (F, "%d %d
", &mapH, &mapW) != 2) addlog(“Invalid map file”);

when u say if(fscanf (F, "%d %d
", &mapH, &mapW) != 2) does not eaqual 2?

Thanq.

fscanf return the number of %* he succeded to scan
if he returns 2 that means you have your height and your width

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?

THanq for taking ur time to explain this to me.

Regards,
Tim

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.

Regards,
Tim

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