0 Members and 1 Guest are viewing this topic.
111111111111111111111111110000000000000000000000011000000000000000000000001100000000000000000000000110000000000000000000000011000000000000000000000001100000000000000000000000110000000000000000000000011000000000000000000000001100000000000000000000000110000000000000000000000011000000000000000000000001100000000000000000000000110000000000000000000000011000000000000000000000001100000000000000000000000110000000000000000000000011000000000000000000000001100000000000000000000000110000000000000000000000011000000000000000000000001100000000000000000000000110000000000000000000000011111111111111111111111111
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}
for (x = 0; x < map_width; x++){ for (y = 0; y < map_height; y++) { printf("%d", map[x][y]); }}
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:Code: [Select]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;
MapData[i] = (int)mapblock[i];
int map[24][26];
int map[26][24];