0

I'm writing a game in SDL2 for a school project, in C, I have a config that lists key-values pairs as such:

groundTiles: images/Overworld/groundTiles.png and cellHeight: 32

How should I go about parsing this data? Because my attempts result in the integers being read correctly but strings are either missing chars or are completely corrupt. I'm somewhat of a beginner to C, at least in terms of file i/o

I need another set of eyes on this code because I've spent too many hours on this already.

Could it have something to do with this struct in my header and how I'm using it to store temporary data?

typedef struct TileMapData_S 
{
    Uint32 col, row, cellWidth, cellHeight, numCells;
    char *mapName;
    char *emptyTileName;
    Bool flag;
    SDL_Color *colors;
    Tile* tileTypes;
    char *colorMap;
}TileMapData;

I've tried making it an unnamed struct in the function, then the source. No luck. I tried just not using a struct and fscanf'ing each piece of data into a separate variable. Same thing, no luck. If I did fscanf(file, "%s %s", buf, temp) with temp being the value of the key I'm parsing, then I get the first encounter of the string I'm looking for, then it copies itself to the other two char* that are holding the names of my sprites/files.

EDIT: This is my attempt based on comments, which does not work, any insight would be appreciated

    while (!data->flag)
    {
        while (tempString != EOF)
        {
            tempString = strtok(buf, " \n");

            if (strcmp(tempString, "width:") == 0)
            {
                tempString = strtok(buf, "\n\0 ");
                map->numColumns = atoi(tempString);

                continue;
            }
            .
            .
            .
            if (strcmp(tempString, "groundTiles:") == 0)
            {
                data->mapName = strtok(buf, "\n\0 ");

                data->mapName = tempString;

                if (data->mapName != NULL)
                {
                    data->flag = true;
                }
                else
                {
                    data->flag = false;
                }

                continue;
            }
            .
            .
            .

            tempString = fgets(buf, sizeof(buf), file);
            slog(buf);
        }

        rewind(file);
    }

I was expecting to get the string I wanted, without the whitespace/null-terminating char, but ended up with an infinite loop

END EDIT

I expect that when I parse groundTiles: images/Overworld/groundTiles.png using fscanf(file, "%s", buf), doing strcmp on that and a known string (groundTiles:), then a second fscanf should provide the string images/Overworld/groundTiles.png

Ian Rosenberg
  • 53
  • 2
  • 10
  • 2
    This task will be much easier if you don't use `fscanf` at all. Use `fgets` to read entire lines of your config file; parse key-value pairs with `strchr` and/or `strsep`; convert decimal numbers to machine integers with `strtol`. – zwol Apr 19 '19 at 16:22
  • I will try this! – Ian Rosenberg Apr 19 '19 at 16:22
  • Remember to [Remove trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) for string values, and you can also get `strtok` to do this by choice of the delimiter character set. – Weather Vane Apr 19 '19 at 16:24
  • How should I use fgets to loop through a file? I tried doing it at the end of my loop, but I go through one iteration then get an infinite loop. – Ian Rosenberg Apr 19 '19 at 17:02
  • Much simpler and more extensible to use `re2c`. Maybe http://re2c.org/examples/example_13.html? – Neil Apr 22 '19 at 22:18

0 Answers0