#### Archived

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

# How to read from a file??

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

## Recommended Posts

If i were to write a 10*10 map in Notepad and saved it as map.txt Let s = Sand Let r = Rock Let t = Tree this is what i think the map.txt should contain to make my map! s,s,s,s,s,s,s,s,s,s, s,s,s,r,s,s,s,s,s,s, s,s,s,s,s,s,s,t,t,t, s,s,t,s,s,s,s,s,s,s, s,s,s,s,s,s,s,r,s,s, s,s,s,s,s,s,s,s,s,s, s,s,s,r,r,s,s,s,s,s, s,s,s,s,r,s,s,s,s,s, s,s,s,s,r,s,s,s,s,s, s,s,s,s,s,s,s,s,s,s, My code to take it in would look something like for (int x=0, x<10, x++) { for (int y=0, y<10, y++) { tilemap[x][y]= ("the relevent bit of code to copy each value in to the right space in the tile map array"); } } i know this is a very simple way of doing a map but i want to start small....but cant figure out how to copy the relevant value from the .txt file to the tilemap array!! Does it matter that my lines in the file only go 20 characters across? How would i get it to go down to the new lines so it wont be feeding blanks into the array and also how to handle the commas between each letter. Is using letters a bad way to represent your tile, i noticed alot of people use numbers! Thanks Brian

##### Share on other sites
OK the following code is off the top of my head, so if there are any problems it should be easy to fix.

I''m assuming that your file has 2 bytes at the end of a line (CF + LL).

  void ReadFile(){ int x,y; FILE *fin; fin = fopen("map.txt","rb"); if( fin == NULL ) { // Error has occured } else { for (x=0, x<10, x++) { for (y=0, y<10, y++) { tilemap[x][y] = fgetc(fin); fgetc(fin); // Catch that comma } fgetc(fin); // Catch CF fgetc(fin); // Catch LF } fclose(fin); }}

As for the simple way of doing tiles, it''s not have a search on the web for a game called Kye, it uses text files for it''s maps, and a game I''m writing uses this technique as well.

Using letters gives you an extra 16 tiles (a-z) or 42 (a-z,A-Z) or even 52 (0-9,a-z,A-Z) compared to just using numbers (0-9), assuming that you are using one digit per tile. If you what to use more then you would have to write a piece of code that reads until a comma and then returns the number read.

Oh about the code above you might want to place an offset on the tiles so that a=0,b=1,c=2, etc this will help your tile rendering code a bit as well.

When I find my code in tons of trouble,
Friends and colleages come to me,
Speaking words of wisdom:
"Write in C."

##### Share on other sites
Why are you making a home-brew map engine for a game when there''s already plenty good ones out there? Search the web for "Mappy" - it''s a free program that lets you create maps and integrate it into a game. I highly reccomend it. It supports animated, isometric, and hexogonal tiles.

##### Share on other sites
you guys really don''t know what "self-satisfaction" means... a self-written working piece of code, now matter how simple, makes you happier and more satisfied than any alreadywrittensupermegaengine... that''s my opinion...

##### Share on other sites
What''s wrong with good ol'' binary?

##### Share on other sites
If you are using C++, you could alter your map slightly and it is very easy.
The map:
sssssssssssssrssssssssssssstttsstssssssssssssssrsssssssssssssssrrsssssssssrsssssssssrsssssssssssssss

Here is the code to load it:
  char map[10][10];int i,j;fstream in("map.txt",ios_base::in);for(i=0;i<10;i++) { for(j=0;j<10;j++) { in >> map[i][j]; }}in.close();

If you want your map to be int instead of char you can:
  int map[10][10];char temp;int i,j;fstream in("map.txt",ios_base::in);for(i=0;i<10;i++) { for(j=0;j<10;j++) { in >> temp; if(temp==''s'') { map[i][j]=1; } else if(temp=''r'') { map[i][j]=2; } else if(temp=''t'') { map[i][j]=3; } }}in.close();

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home

##### Share on other sites
Or if you liked, I could post the source to a Tokenising library I wrote for things just like this.

You''d read the file into a char*[] array

And call my class CTokens.Tokenise(char*).

This then creates a list of tokens that you can scroll through. Useful in IRC and other type libraries

##### Share on other sites
Bye a book!! LOL!!

I own loads of books...been in college for last 4 years doin a Bs in Applied Computing...

Now my Head is fried...just 2 things i need before i can reach the next level and have my map displayed...

Get rid of that DirectX 7 error that keeps occuring because i''m using DirectX 8 SDK and that little bit of code to read the map in!!!!

##### Share on other sites
Bye a book!! LOL!!

I own loads of books...been in college for last 4 years doin a Bs in Applied Computing...

Now my Head is fried...just 2 things i need before i can reach the next level and have my map displayed...

Get rid of that DirectX 7 error that keeps occuring because i''m using DirectX 8 SDK and that little bit of code to read the map in!!!!

Then i can move on and start the game logic...been stuck here a while...so need to just get it out of the way!!!

Have a load of other Projects that i''m trying to get through as well as this.. i''ve little time to struggle with something that is quiet simple and obvious to others and i can be simply helped with!!

If you dont want to reply Pyabo then dont, everything is not as black and white as you''d se it!.
If you do not want to reply then dont! Thanks.

Why not use Mappy...well because would not be appreciated by my Lectures and that means i''d fail!!

"self-satisfaction" I''ve no time for satisfaction tryin to get true this cousrse!!

Thanks Steven and Captain Jester!!

How come you have no commas in you map... you dont need them then i take it :-D

Converting to ints what is the benefit!!

Thanks for all your help... will take alook at Kye aswell!

##### Share on other sites
quote:
Original post by Woody FX
How come you have no commas in you map... you dont need them then i take it :-D

You don't need commas, because the fstream takes care of it. Since it is only looking for a single char, then it only loads a single char. If it was looking for an int, then it would load an int. Also, just a little fix on that, you may want to put spaces in between each one. Spaces act like a delimeter to fstream, so if you load ints, then it will use a space as a boundry to separate them.
Like this:
564738
Would load as a single int.
5 6 4 7 3 8
Would load as 6 separate ints.

The reason it works for char without the spaces because a char is only 1 byte and text files save information by the byte.

quote:
Original post by Woody FX
Converting to ints what is the benefit!!

Thanks for all your help... will take alook at Kye aswell!

I put the conversion to ints because your original post had the map as a set of ints.

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home

Edited by - CaptainJester on January 31, 2002 12:09:08 PM

##### Share on other sites
the char to int conversion can be done much simpler:
  int Map[10][10];...in >> (char)Map[i][j];Map[i][j] -= ''a'';...

##### Share on other sites
why need a file? just hardcode that level directly in your code!

##### Share on other sites
You''ve been in college for four years studying "Applied Computing" and you don''t know how to read data from a file? Excuse me for being a total prick, but just what kind of classes are you taking? What college is this?

You own lots of books, but none of them have a chapter called "File I/O" or "How To Use Files?" Are any of your books basic C/C++ tutorials?

*baffled*

Lucky for you, there are plenty of other GameDev readers who aren''t as mean as I am. Now go READ those books.

##### Share on other sites
quote:
Original post by Pyabo
You''ve been in college for four years studying "Applied Computing" and you don''t know how to read data from a file? Excuse me for being a total prick, but just what kind of classes are you taking? What college is this?

Don''t worry, there are probably worse ones than him.

Well anyway

a.txt
Remove the comma''s, don''t need them, will only slow down map loading.
Secondly, my version has a number on the first line. This tells it the map size.
10 means 10x10 map
30 means 30x30 map (make sure you make it a 30x30 map though)
and so on
  10sssssssssssttttsttssstsststttsststtstttssttttsttttsttststtttsttttsttttsttststtttsttttsttttststtstsss

a.cpp
Complete example, compile and run.
s for sand (dots) and t for trees (question marks)
reason for out put like this? dots (.) look like sand and question marks (?) look like trees (use your imagination).
 ` #include #include #define MAX_DIM 256char map[MAX_DIM][MAX_DIM];int MAPDIM;void readmap(char *s){ FILE *in; int i; char string[256]; in=fopen(s,"rb"); MAPDIM=atoi(fgets(string,255,in)); for(i=0;i

If you didn''t have the carriage returns at the end of the file, it could be loaded in one call.

Beer - the love catalyst
good ol'' homepage

##### Share on other sites
Why dont i know file i/o...... thats a good question i''ve done c++ progs about 2 years ago using alot of file i/o but i''m getting such a wide breath of subjects fired at me to fairly deep level that that things are getting daunting quiet quickly and things dont stick for long then...i''ve 6 subjects to study and a year long project and a load of other smaller projects to do for every each subject on the course, yesterday i got a prolog project (A.I.)... Write a Gin-Rummy game .... you dont have time to go over everything you learn so you forget things soon...have not done C++ in the last 2 years so things get hazy quickly... No Paybo i dont thing your a Prick... you just need to find a lady to let all the frustration out ;-D

Thanks for all your help... and in the future i''ll be here to help others( once i learn something)

Brian

##### Share on other sites
not using C/C++ is a feasable and forgivable excuse

have fun

Beer - the love catalyst
good ol'' homepage

• ### Forum Statistics

• Total Topics
628701
• Total Posts
2984289

• 22
• 10
• 9
• 13
• 13