Archived

This topic is now archived and is closed to further replies.

Spoons

Map Loading from a .txt file... (yes i googled)

Recommended Posts

I have googled this, searched through my c++ reference book, and tried to search the forums (they are down atm). I can''t seem to figure out how to read from a .txt file, letter by letter. I have tried: ifstream MapFile("Map.txt"); char String[256]; int x; MapFile >> x; sprintf(String, "%i", x); Log.Print(String); This doesn''t seem to work. It gives me a number similar to the address of the variable (2011667013 to be exact). It seems like this should work, but it doesn''t? This is how it tells me to do it, everywhere I have looked. I store my maps like this: 11111 10001 10001 11111 Each number respresenting a different texture. It works if I hardcode it into the array, but I need to know how to load it from a .txt file, and then assign it to every value in my array. Any help is GREEEEEEEATLY appreciated.

Share this post


Link to post
Share on other sites
FILE *f=fopen("blah.txt","rt");
char sz[1024];
while(!feof)
{
fgets(sz,1024,f);
Interpret(sz);
}

fclose(f);

interpret does nothing else than looking through each char and checking if it is ''1'' or ''0'' and it does this until it finds a \n or if the char ==0
0 (terminated string)
thats it

Share this post


Link to post
Share on other sites
Invader X, I used your method, and it printed back the number 49. I will show you exactly what I did.

ifstream MapFile("Map.txt");
char MapInfo[256];
int i;

i = MapFile.get();
sprintf(MapInfo, "%i", i);
Log.Print(MapInfo);


And map.txt looks like this:

111111111
100000001
100000001
100000001
111111111

Share this post


Link to post
Share on other sites
Here's a straight way to do it simple and it works...


FILE *f=fopen("filename.txt","r");
char x;
int n;
while(!feof(f))
{
x = getc(f);
n = atoi(&x);
printf("%d\n",n);
}
fclose(f);


Are you sure you googled? I found this pretty fast click me!

[edited by - FtMonkey on July 3, 2003 12:49:22 AM]

[edited by - FtMonkey on July 3, 2003 12:52:57 AM]

Share this post


Link to post
Share on other sites
Try something like this... I hope it makes sense lol.

#define MAX_MAP_WIDTH 10
#define MAX_MAP_HEIGHT 10
int Map[MAX_MAP_WIDTH][MAX_MAP_HEIGHT];
char Line[MAX_MAP_WIDTH];
int i;

ifstream MapFile("Map.txt");
for(int y = 0; MapFile.eof() == false && y < MAX_MAP_HEIGHT; y++)
{
MapFile.getline(Line, MAX_MAP_WIDTH);
for (int x = 0; Line[x] != NULL && x < MAX_MAP_WIDTH; x++)
if ( isdigit(Line[x]) )
Map[x][y] = Line[x] - ''0'';
}

Share this post


Link to post
Share on other sites
Thank you FtMonkey, that is exactly what I needed. Now one more question... I am also having trouble loading each number into an array. This is my current method:


for(int y = 0; y<MAP_SIZEY; y++)
{
for(int x = 0; x<MAP_SIZEX; x++)
{
x = getc(f);
n = atoi(&x);

map[y][x] = n;
}
}


Now the map seems to have textures scattered all over the place. Is there something I am doing wrong?

[edited by - Spoons on July 3, 2003 12:58:44 AM]

Share this post


Link to post
Share on other sites
Oh sorry my fault, the problem is you are reading in the 0 at the end of the line...so it gets messed up so you have to take out the 0 and use another number here's a modified version sorry I didn't test my code before I apologize once again.

  
FILE *f=fopen("numbers.txt","r");
char x;
int n;
for (int pos_y=0;pos_y<5;pos_y++)
{
for(int pos_x=0;pos_x<5;pos_x++)
{
x = getc(f);
n = atoi(&x);
if(n==0) printf("\n");
if(n!=0)
printf("%d",n);
}
}
fclose(f);


Just make a simple text file 5x5 and run this example (without using 0) so you see what I mean. Also if you had spaces between the numbers it would read a 0 which is an empty character so you need to find a way around this. You can also get it string by string and convert each one to an int which is sorta better.

[edited by - FtMonkey on July 4, 2003 1:19:51 AM]

[edited by - FtMonkey on July 4, 2003 1:20:45 AM]

[edited by - FtMonkey on July 4, 2003 1:48:23 AM]

Share this post


Link to post
Share on other sites
Okay here's an example on how to apply what I said:


int array[6][6]; //this is your map

FILE *f=fopen("numbers.txt","r");
char x;
int n;

//first we get the data into the array

for(int pos_y=0;pos_y<6;pos_y++)
{
for(int pos_x=0;pos_x<6;pos_x++)
{
x=getc(f);
n=atoi(&x);

//store data into your array

array[pos_y][pos_x]=n;
}
}

//now to draw your data

for(int loop_y =0;loop_y<5;loop_y++)
{
for(int loop_x=0;loop_x<5;loop_x++)
{
printf("%d",array[loop_y][loop_x]); //we draw our data

}
printf("\n"); //jump to next line

}

fclose(f);


There I hope I got that right... :D

[edit] fixed the code a little bit

--------------------------------------------------------
The monkeys are listening...

[edited by - FtMonkey on July 4, 2003 2:14:01 AM]

[edited by - FtMonkey on July 4, 2003 2:33:13 AM]

Share this post


Link to post
Share on other sites
BYTE a[128][128]

BYTE *p=a;

now lets say you want to add element 128 to your array

p=p+127;
p=elementvalue;

note an array goes from 0->arraysize-1


so lets say you have element n
int i=n/128;
n-=i*128;

BYTE *p=a;
p=p +i*128+n;
p=value;
maybe there are some little bugs with the pointer shifting values it s very early today so i am still tired

but thats the way i usually do it

Share this post


Link to post
Share on other sites