c - fscanf 读取键值对时出现问题

标签 c scanf

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

我对 C 有点陌生,文件 I/O 对我来说一直是一个问题。我将如何解析“keyName:valueName”形式的键值对?我的问题开始发挥作用,因为我的值可能是字符串、 float 或无符号整数。

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

玩家. 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;
    }
  } 
}

最佳答案

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); 您需要传递地址。 – 约翰尼·莫普

关于c - fscanf 读取键值对时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54653851/

相关文章:

c - C 中的优先级队列实现 - 将字符更改为整数

c - Sscanf 没有返回我想要的

C: char 的 scanf 没有按预期工作

c - 我怎样才能接受多个客户端到 TCP 服务器?

c - 为文件夹 C 语言中的文件名过滤 scandir

c - scanf() 将换行符保留在缓冲区中

c - sscanf : get first and last token in a string

c++ - C/C++ Hex char* 到字节数组

c++ - 在 C++ 中替代 sscanf_s

c - 尝试缓冲区溢出