0
votes

EDIT:Since it was asked, I'm using Visual Studio 2013 Ultimate and it has raised no warnings.

I'm somewhat new to C, and file i/o has always been an issue for me. How would I go about parsing key value pairs in the form of "keyName: valueName"? My issue comes into play because my values could be a string, a float, or an unsigned int.

I'm writing a game using SDL2 and I'm trying to keep as much of the config for individual actors in separate .actor files. Following some sample code I've managed to read the key portion of the pair via fscanf, however when I attempt to fscanf the value in to my actor I get an exception.

Player.actor

folder: images/Player/
scale: 1.0,1.0
color: 255,255,255,255
colorSpecial: 10,255,100,255
xOffset: 1
numAnimations: 3

name: idle
filename: playerIdle.png
length: 8
frameWidth: 39
frameHeight: 87
frameRate: 0.75
type: loop

name: attack
filename: playerPunch.png
length: 8
frameWidth: 50
frameHeight: 82
frameRate: 0.75
type: once

name: walk
filename: playerWalk.png
length: 7
frameWidth: 50
frameHeight: 82
frameRate: 0.85
type: loop

Code:

void LoadActor(Actor *actor, char *filename)
{
  FILE * file;
  char buf[512];
  char* imageFile = "";
  int i = 0;

  file = fopen(filename, "r");

  if (!file)
  {
    return NULL;
  }

  rewind(file); 

  while (fscanf(file, "%s", buf) != EOF) //this works
  {
    if (strcmp(buf, "numAnimations:") == 0) // this works
    {
        fscanf(file, "%i", i); // this doesn't?

        continue;
    }
  } 
}
1
fscanf(file, "%i", &i); You need to pass the address.Johnny Mopp
You should be getting a warning for that. What is your compiler?Andrew Henle
the rewind(file); is useless, you just open the filebruno
why the continue in the while ? you do not need it to loop ^^bruno
Visual Studio 2013 Ultimate and I didn't notice I still had that continue there.Ian Rosenberg

1 Answers

0
votes

when I attempt to fscanf the value in to my actor I get an exception.

       fscanf(file, "%i", i); // this doesn't?

fscanf(file, "%i", &i); You need to pass the address. – Johnny Mopp