Home Forums Programming Reading Map file – C++

Viewing 4 reply threads
  • Author
    • #7408

      I currently have a map file in a txt format. such as the below code

      [code:1:1cc512fea1]1:0 0:0 0:0 1:0[/code:1:1cc512fea1]

      I need a way to separate them out without using fscanf().
      For example the way my map format works is the space is the delimiting character and then the first number on the left hand side of the colon is the TileID and then the number the other side is TileType.

      I have tried to use getline() but it doesn’t allow the same functionalty as fscanf(); Such as the code below

      [code:1:1cc512fea1]fscanf(FileHandle, "%d:%d ", &tempTile.TileID, &tempTile.TypeID);[/code:1:1cc512fea1]

    • #44550

      Hi Ciaran,

      I’d be inclined to store this map data in XML or INI file and use (or write) a respective parser.

      I guess it depends on the scope of the project. Are you familiar with the concept "Data Driven Design?"

      If not, maybe give this a read:


      Also found this, which seems closer to the solution that your looking for:


      ifstream f("data.txt");
      string str;

      while ( getline(f, str) )
      Point p;
      sscanf(str.c_str(), "%f, %f, %f\n", &p.x, &p.y, &p.z);


      Source: http://stackoverflow.com/questions/536148/c-string-parsing-python-style


    • #44553

      Thanks very much for your reply

      I have read some of that book before alright. I do take that approach as much as I can. Like for example in this project to load images into memory and onto a surface (using SDL by the way) I use a class method that reads the names of the images from a text file thus adding new titlesets without having to recompile etc.

      Also that code solved my problem, its always something so simple.
      I was using the below function

      [code:1:9229f9810f] fscanf()[/code:1:9229f9810f]

      And it was throwing an error that I needed a FILE* and I was using ifstream.
      Its so obvious that the f stands for file and in sscanf() s for string or maybe I am wrong but I think I understand it all now.

      while ( getline(myfile, TempContainer, ‘ ‘ ) )

      sscanf_s(TempContainer.c_str(), "%d:%d ", &tempTile.TileID, &tempTile.TypeID);

      CError::Report("pushback completed");

      Above is the code I used in the end, the compiler told me to use sscanf_s() instead of sscanf() due to a security hole in the function or something to that effect.

      Thanks again

    • #44557

      Glad to be of help.

      As for the "_s", that tends to stand for "secure", its the same function, but usually takes an additional size parameter to avoid buffer overflows.

      So when do we get to play this game of yours???


    • #44559

      I see, thanks for the info.

      Oh well now that might be a long time, I am just developing the engine atm, well if you could call it that. So far I have the map loading and titling systems done. Moving onto event management and animation next, then collisions.

      Fairly new to SDL so trying to learn by doing. Probably doing it all wrong, and probably have loads of loss pionters ha but it works.

      I posted a video on youtube of what I got to so far.

Viewing 4 reply threads
  • The forum ‘Programming’ is closed to new topics and replies.