Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on October 22, 2011, 02:06:01 pm
-
I have an issue that's been annoying me for some time now. I have to read a file to an bi-dimensional array.
The file looks like this:
1111111111111111111111111
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1111111111111111111111111
I am using the C library ('stdio.h') to read the file, and this is what I have so far.
int map[26][24];
int map_width = 26;
int map_height = 24;
int x;
int y;
FILE *map_file = fopen("map.txt", "r");
for (x = 0; x < map_width; x++)
{
for (y = 0; y map_height; y++)
{
fscanf(map_file, "%d", &map[x][y]);
}
fscanf(map_file, "\n"); // Skip newlines
}
However, it doesn't really seem to be working, since I'm printing the array right after and it doesn't look like the file:
for (x = 0; x < map_width; x++)
{
for (y = 0; y < map_height; y++)
{
printf("%d", map[x][y]);
}
}
Thanks for the help!
-
Are you casting to an int after you read from the file? Because that might be the problem.
In Daemons I read from a binary file and I read to a memblock equal to the size of the file - 2 bytes for size data.
I then copy that memblock to a vector for use in the game. ;)
Perhaps you could try that. (Is this only going to be C or C++?)
EDIT: Here's my code:
fstream map;
string map_path;//Holds the path to the files
try
{
map_path = "data/maps/visual/" + mapID;
map.open(map_path.c_str(), ios::in|ios::binary);
char memblock[2];
//char* mapblock;
if(map.is_open())
{
//memblock = new char [2];
//map.seekg(0, ios::beg);
map.read(memblock, 2);
MapWidth = memblock[0];
MapHeight = memblock[1];
//delete[] memblock;
char mapblock[MapWidth * MapHeight];
MapData.resize(MapWidth * MapHeight);
//map.seekg(2, ios::beg);
map.read(mapblock, MapWidth * MapHeight);
for(int i = 0; i < (MapWidth * MapHeight); i++)
MapData[i] = (int)mapblock[i];
//cout << MapData[0];
//cout << map_path.c_str();
map.close();
}
}
catch(exception& e)
{cout << e.what() << "\n";}
return;
-
Are you casting to an int after you read from the file? Because that might be the problem.
In Daemons I read from a binary file and I read to a memblock equal to the size of the file - 2 bytes for size data.
I then copy that memblock to a vector for use in the game. ;)
Perhaps you could try that. (Is this only going to be C or C++?)
EDIT: Here's my code:
fstream map;
string map_path;//Holds the path to the files
try
{
map_path = "data/maps/visual/" + mapID;
map.open(map_path.c_str(), ios::in|ios::binary);
char memblock[2];
//char* mapblock;
if(map.is_open())
{
//memblock = new char [2];
//map.seekg(0, ios::beg);
map.read(memblock, 2);
MapWidth = memblock[0];
MapHeight = memblock[1];
//delete[] memblock;
char mapblock[MapWidth * MapHeight];
MapData.resize(MapWidth * MapHeight);
//map.seekg(2, ios::beg);
map.read(mapblock, MapWidth * MapHeight);
for(int i = 0; i < (MapWidth * MapHeight); i++)
MapData[i] = (int)mapblock[i];
//cout << MapData[0];
//cout << map_path.c_str();
map.close();
}
}
catch(exception& e)
{cout << e.what() << "\n";}
return;
Actually, the program is C++, but I thought C would be better for this, due to speed and size issues, I want map loading to be fast.
What do you mean by "Are you casting to an int after you read from the file? Because that might be the problem."?
-
You are reading from a text file, so what you are reading is going to be a char data type. However, the array is an int so there's going to be issues there. I use the code:
MapData[i] = (int)mapblock[i];
to fix that in my file loading as it changes the char to an int. (Also, the function posted above is quite fast, speed won't be a problem between C/C++) ;)
-
It should be
int map[24][26];
instead of your int map[26][24];
.
Also there's a typo: for (y = 0; y < map_height; y++)
And I think you have to exchange the x and the y loop.
Correct me if I'm wrong.