FILE *f;
f=fopen("maps/desert.map","r");
if (f==NULL) perror ("Error opening file desert.map");
else
{
fseek (f, 0, SEEK_END);
size=ftell (f);
bytecount = size;
fseek (f, 0, SEEK_SET);
}
int rows, columns;
int temp;
for(rows=0; rows<25; rows++)
{
for(columns=0; columns<34; columns++)
{
temp = fgetc(f)-48;
map[rows][columns] = temp;
printf("%i", map[rows][columns]);
}
printf("\n");
temp = fgetc(f)-48;
}
So now to my problem. The "rows<25" and "columns<34", i want them to be read directly out of the file so its automatic so if this is my map file: 2222222222 2222222222 2222222222 2222222222 2222222222 (COLUMNS: 10, ROWS:5) the "25" in the for and the "34" in the for loop, shall be "10" and "5" Get it? Otherwise for example, if it was "for(columns=0; columns<20"; columns++)" The map would look like this: 22222222222222222222 22222222222222222222 2222222222 Get it? thanks.
problem with map file
Hellu there! This is map.map file: 1111111111111 2222222222222 1111111111111 and so on, where 1=WATER, 2=GRASS and so on... some code:
There are many ways to do this but a couple of ways to do this might be:
Most common is to just store the array size at the beginning of the map file:
5 4
11111
22222
11111
12121
Another way is to count the number of characters in the first line until the newline to get the number of collumns and then count the total number of newlines to get the number of rows.
Hope that helps
Most common is to just store the array size at the beginning of the map file:
5 4
11111
22222
11111
12121
Another way is to count the number of characters in the first line until the newline to get the number of collumns and then count the total number of newlines to get the number of rows.
Hope that helps
Evillive2
Using evillive2's map:
5 4
11111
22222
11111
12121
char buffer[255];
fgets(buffer, 255, f);
int ncolumns, nrows;
sscanf(buffer, "%d %d", &rows, &ncolumns);
5 4
11111
22222
11111
12121
char buffer[255];
fgets(buffer, 255, f);
int ncolumns, nrows;
sscanf(buffer, "%d %d", &rows, &ncolumns);
Quote:
Zeophlite:
char buffer[255];
fgets(buffer, 255, f);
int ncolumns, nrows;
sscanf(buffer, "%d %d", &rows, &ncolumns);
There are many tutorials and source code examples on the web for C style file I/O routines. Here is a modified one for reading in a number from a file:
// text mode file I/O. Read a single integer from a file.// Building on this function floating point numbers are simple to add.int fread_integer( FILE *fp ){ int number; bool sign; char c; // skip leading spaces do { c = getc( fp ); } while ( isspace(c) ); number = 0; // check sign sign = FALSE; if ( c == '+' ) { c = getc( fp ); } else if ( c == '-' ) { sign = TRUE; c = getc( fp ); } // check if it is a digit if ( !isdigit(c) ) { // options: try to continue or exit // this is a good reason to have some error logging // system return 0; } // read in the digits while ( isdigit(c) ) { // this works for base 10 numbers // and isnt' that much different for // numbers after a decimal point number = number * 10 + c - '0'; c = getc( fp ); } // modify the sign of the number if ( sign ) number = 0 - number; // again, this is where error logging comes in handy // also, this is where you can expand upon reading in digits // AFTER a decimal point. if ( c != ' ' ) ungetc( c, fp ); // the payoff return number;}
I don't make any claims that this is the best way to do things, just one that works and is very easy to use. If you leave a space between all the numbers it would look somethingl ike this:
FILE *fp;int width, height;int row, col;fp = fopen( "map.map", "r" );width = fread_number(fp);height = fread_number(fp);for ( row = 0; row < height; row++ ) for ( col = 0; col < width; col++ ) map[col][row] = fread_number(fp);
In all fairness even though text files are great for flexibility and readablility, once you get into larger maps the text files can become rather huge rather quickly. Since you will probably create an editor later on, the readability won't be as much of an issue but for some people the download size will be. Then, binary files will provide you with a few things, smaller file size, faster parsing (in many cases anyway, this one in particular), and hiding the data you might not want changed from every day average joe user.
Hope that helped.
p.s. I didn't develop that code snippet above. I am not sure who contributed the original but a similar version can be found in almost any Merc Diku MUD derivitive which are available for download and use from many places such as ftp.game.org.
Evillive2
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement