简体   繁体   English

使用fscanf从包含整数数组的文件中读取

[英]using fscanf to read from a file that contains an integer array

I'm trying to read a JSON-format file that contains an integer array(eg [ 0, 1, 2, 3, 4 ]) I am wondering why fscanf skips the brackets in the file and go straight to the numbers when I use 我正在尝试读取一个包含整数数组(例如[0、1、2、3、4])的JSON格式的文件,我想知道为什么fscanf在使用时跳过文件中的括号并直接转到数字

   // the type of value is integer
  FILE* fp=fopen(file,"r");
  fscanf( fp,"%d",&value);

I'm still new to file I/O and I have no idea why this happens. 我仍然是文件I / O的新手,我也不知道为什么会发生这种情况。 I thought whenever I call fscanf, the file pointer would move 1 position forward. 我以为,每当我调用fscanf时,文件指针都会向前移动1个位置。

You should check return values. 您应该检查返回值。 The fscanf in your example should return 0 since the first non-white-space character encountered is [ which can not start a number so that parsing fails there. 您的示例中的fscanf应该返回0,因为遇到的第一个非空白字符是[不能以数字开头,因此解析失败。 The assumed return value of 0 indicates that no succcessful conversion took place. 假定返回值为0表示没有成功进行转换。 The value of value will probably stay unchanged (I couldn't find a specific statement for that in the man page). value的value可能会保持不变(我在手册页中找不到关于此的特定声明)。 The reading position in the file will be before the [ so that subsequent attempts to read an int from the file will fail as well. 文件中的读取位置将位于[之前,因此后续尝试从文件读取int的尝试也会失败。

How to read the json array: 如何读取json数组:

Note that I do not handle errors in the examples below. 请注意,在以下示例中,我不处理错误。 You must do that though... 您必须这样做...

scanf format strings can can contain conversion specifications which contain a list of allowed or forbidden characters. scanf格式字符串可以包含转换规范,其中包含允许或禁止的字符列表。 This can be used to read "away" anything which is not a number: char buf[some large enough value]; fscanf(" %[^0-9]", buf); 这可以用来读取“无”的任何非数字的内容: char buf[some large enough value]; fscanf(" %[^0-9]", buf); char buf[some large enough value]; fscanf(" %[^0-9]", buf); . The reading position is now before the first number. 读取位置现在在第一个数字之前。

Then you'll have a loop doing two things: 然后,您将有一个循环执行以下两项操作:

The number ahead of us can be read trivially with fscanf("%d", &value); 可以使用fscanf("%d", &value);轻松读取前面的数字fscanf("%d", &value); . This will also skip possible whitespace before the number in later iterations. 这还将在以后的迭代中在编号之前跳过可能的空格。 Now we must deal with the comma: fscanf(" %[,]", buf); 现在我们必须处理逗号: fscanf(" %[,]", buf); ("read a comma which is optionally preceded by white space"). (“读取逗号,并可选地在其前面加上空格”)。 Now you can read the next number. 现在,您可以阅读下一个号码。

The last number will not be followed by a comma. 最后一个数字后面不会有逗号。 The attempt to read a comma will therefore fail (ie return 0); 因此,读取逗号的尝试将失败(即返回0); this can be used as end-of-array indicator. 可以用作数组结尾指示符。

If more arrays or other stuff may follow you must read away the remaining whitespace and closing square bracket so that you leave the file position after the array for others. 如果可能会出现更多数组或其他内容,则必须清除剩余的空格和右方括号,以便您将文件位置留给其他数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM