读取键值对的fscanf问题

时间:2019-02-12 15:47:30

标签: c scanf

编辑:自从被询问以来,我正在使用Visual Studio 2013 Ultimate,它没有发出警告。

我对C有点陌生,文件I / O一直是我的问题。我将如何解析“ keyName:valueName”形式的键值对?我的问题之所以起作用,是因为我的值可以是字符串,浮点数或无符号整数。

我正在使用SDL2编写游戏,并且试图将单独角色的尽可能多的配置保留在单独的.actor文件中。按照一些示例代码,我设法通过fscanf读取了该对的关键部分,但是当我尝试将fscanf的值输入到actor中时,我得到了一个异常。

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

代码:

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 个答案:

答案 0 :(得分:0)

  

当我尝试将值fscanf输入到演员中时,我得到一个异常。

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

fscanf(file, "%i", &i);您需要输入地址。                     –约翰尼·莫普(Johnny Mopp)

相关问题