fscanf读取键值对的问题

问题描述 投票:0回答:1

编辑:因为它被问到,我使用的是Visual Studio 2013 Ultimate,它没有提出任何警告。

我对C有点新,文件i / o对我来说一直是个问题。我将如何以“keyName:valueName”的形式解析键值对?我的问题发挥作用,因为我的值可能是字符串,浮点数或无符号整数。

我正在使用SDL2编写一个游戏,我试图在单独的.actor文件中为各个actor保留尽可能多的配置。下面是一些示例代码,我已经设法通过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;
    }
  } 
}
c scanf
1个回答
0
投票

当我尝试fscanf我的演员的价值时,我得到一个例外。

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

fscanf(file, "%i", &i);您需要传递地址。 - 约翰尼莫普

© www.soinside.com 2019 - 2024. All rights reserved.