|
-
Nov 25th, 2002, 03:39 PM
#1
Thread Starter
Fanatic Member
[Resolved] maps for 2d games
I'm trying to use a simple map writen in a txt file in my game but I can't get it to work.
I have an array of integers: int iMap[10][10];
And then I read in numbers from a txt file useing for loops and useing iMap[x][y] = fin.get()
Here's my problem; When reading the file like this it puts all tbe '/n' in the array as well so I have to have the whole map on one line in the txt file. I would like to have it 10 by 10 in the file as well. How can I tell fin.get() to ignore all linebreaks in the txt file?
Last edited by McCain; Feb 17th, 2003 at 07:18 PM.
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Nov 25th, 2002, 04:34 PM
#2
Frenzied Member
make 2 for loops
one to handle x, and one for y
for(int y=0; y<10; y++)
{
for(int x=0; x<10; x++)
{
assign to myarray[x][y]
}
}
-
Nov 25th, 2002, 04:47 PM
#3
Frenzied Member
Steve, you didnt answer the question =P.
Its actually pretty simple... dont use f.get(). Instead, use the stream operator, like you would if reading an integer from cin. It will use the line feed as a terminator, and move to the next line.
Z.
-
Nov 26th, 2002, 06:53 AM
#4
Thread Starter
Fanatic Member
So I would go fin >> iMap[x][y] ?
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Nov 26th, 2002, 11:58 AM
#5
Can you show us an example file?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 26th, 2002, 03:10 PM
#6
Thread Starter
Fanatic Member
The text (map) file looks like this:
Code:
1111111111
1000000001
1000000001
1000000001
1000000001
1000000001
1000000001
1000000001
1000000001
1111111111
And in my source I have:
Code:
ifstream fin;
char chrMap[10][10];
fin.open("map.txt");
for(int x = 0; x < 10; x++)
{
for(int y = 0; y < 10; y++)
{
fin >> chrMap[x][y];
}
}
And this works pretty well but I want the array to be an integer array and if I just declare it int iMap[10][10]; I get some crazy stuff in that array (stuff like -858993460)... Why does this happen?
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Nov 26th, 2002, 05:54 PM
#7
Because it reads a whole line at once...
Here's what you could do, but it's more C-like, which is why I use the C io functions:
Code:
#include <stdio.h>
#include <ctype.h>
FILE *fin;
char chrMap[10][10];
char c;
int x, y;
fin = fopen("map.txt", "rt");
// note the inversed!
for(y=0;y<10;++y) {
for(x=0;x<10;++x) {
do {
c = fgetc(fin);
} while(!isdigit(c));
chrMap[x][y] = c - '0';
}
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|