   Tile-based mapping: reading from files
  Hi there. I'm making a game in C++ with SDL libraries, and I just started implementing a tile-based map system. I have all the code to check if it's at least working. There's one little thing that I fail to understand. It's probably because I never finished reading my C++ book, but I don't want to read 600 pages to get the answer on 1 question. (yes I will finish the book, just not today)here's what I have so far:FILE *pFile;int a;int b;int c;char d;int k;pFile = fopen ("mapinfo.txt","r");int tx, ty;tx = 0;ty = 0;do{a = fgetc(pFile);b = fgetc(pFile);c = fgetc(pFile);d = fgetc(pFile);a = a * 100;b = b * 10;k = a + b + c;TILE[tx][ty].setfileno(k);if(k == 0){TILE[tx][ty].setwalkable(true);}else{TILE[tx][ty].setwalkable(false);}TILE[tx][ty].setfileno(k);tx++;if(d == '$'){ty++;tx = 0;}}while (a != EOF && b != EOF && c != EOF && d != EOF);The mapinfo.txt looks like this: 000#000#000#000$000#000#000#000$000#000#000#000$000#000#000#000$(with 60 tiles wide instead of 4, just thought this would look less chaotic.I've commented things out and I'm 100% positive that the error is somewhere in this bit of code. Thing is, I practically know nothing of reading from a file.(I know I should use fopen_s but I figured I'd just try it like this first)I'm hoping you can help me. Thanks for your time anyhows :)   
  Oh I forgot to mention I made a tile class, which stores a boolean called walkable and an int called fileno. Walkable is self-explanatory, but fileno determines the rectangle from the original map.bmp file. So if fileno == 0, we'd use the upper tile, if fileno == 1, we'd use the one below that, etc etc. It's how I store my tiles.   ;Quote:Original post by PoitertjuhHi there. I'm making a game in C++... I never finished reading my C++ book, but I don't want to read 600 pages to get the answer on 1 question. (yes I will finish the book, just not today)here's what I have so far:FILE *pFile;Get a better book. In modern C++, we use std::fstream to represent a file, not FILE*. Oh, and in C++, you can (and should) declare variables near first use, not just all at the top of the function.Quote:I've commented things out and I'm 100% positive that the error is somewhere in this bit of code. Thing is, I practically know nothing of reading from a file.It would help to say what's going wrong. Here's what is going wrong, that I can see:1) The end of each line counts as a character, which throws off your parsing that assumes 4 characters per entry. The first entry of the second line will read the return character as the hundreds digit of the next number.2) The character '0' does not have the numeric value zero. It has the numeric value 48. If this doesn't make sense to you, [google] ASCII.3) Your code only checks for the input variables being equal to EOF at the end of the loop. When EOF is returned by the read call, that EOF value will still be used within the remainder of the current loop iteration.Quote:(I know I should use fopen_s but I figured I'd just try it like this first)No. You should use the member functions and operator overloads of a std::fstream object.By the way, there is functionality in both C and C++ that is smart enough to read human-readable numbers out of a text file, so there is no reason to parse the numbers yourself by multiplying and adding.Quote:I'm hoping you can help me.Of course I can help you. Here's modern C++ code that will read a file with numbers on each line - in human-readable format, with whitespace between the numbers, with numbers of any size that will fit in an int - into a container that will automatically expand to whatever size is needed.// You will need:#include <fstream> // for ifstreams#include <sstream> // for stringstreams#include <string> // for strings#include <vector> // for vectorsusing namespace std;// And then you can do something likeifstream file("mapinfo.txt");vector<vector<int> > TILE; // instead of whatever it is currentlystring line;// Notice how the read operation itself is used to control the loop. This is// valid, idiomatic modern C++ code.while (getline(file, line)) {  vector<int> data;  stringstream parser(line);  int value;  while (parser >> value) {data.push_back(value);  }  TILE.push_back(data);}