【问题标题】:fscanf not scanning values properlyfscanf 没有正确扫描值
【发布时间】:2013-10-27 21:28:30
【问题描述】:

我有以下代码:

#include <stdio.h>
#include <string.h>
#define SIZE 6
struct books{
    char name[100],author[100];
    int year,copies;
  };
struct books book1[SIZE],book2[SIZE],*list;

main()
{
  unsigned i;

  char line [ 100 ],temp1[100],temp2[100],c; /* line size */
  size_t len;

  FILE *file;

  file=fopen("input.txt","rw");

  int k=0;
  for(i=0;  fgets ( line, sizeof line, file) ; i++ ) /* read a line */
  {
    len = strlen(line);
    while (len && line[len-1] == '\n') line[--len] = 0;
    switch (i % 4) 
    {
      case 0: /* handle first line here */
             fscanf(file, "%[^\n]s",book1[k].author);
             break;
      case 1: /* handle second line here */

             fscanf(file, "%[^\n]s",book1[k].name);
             break;
      case 2: /* handle third line here */

             fscanf(file, "%[^\n]s",temp1);
             book1[k].year=atoi(temp1);
             break;
      case 3: /* handle fourth line here */

             fscanf(file, "%[^\n]s",temp2);
             book1[k].copies=atoi(temp2);
             break;
    }
    if(i % 4 == 3)
    {
      k++;
    }
  }
  fclose(file);

  i=0;
  while(i<SIZE)
  {
    printf("##########################################################################\n");
    printf("\nAuthor: %s\nBook: %s\nYear of Publication: %d\nNo of Copies: %d\n\n",book1[i].author,book1[i].name,book1[i].year,book1[i].copies);
    printf("##########################################################################\n");
    i++;
  }

}  

输入文件为:

A
Ruby On Rails
2004
100
J
Learn Python Easy Way
1967
100
D
Harry Potter and the Sorcerer's stone
2012
150
D
Harry Potter and the Sorcerer's stone
3045
140
Z
Harry Potter and the Sorcerer's stone
2013
150
K
Inferno
1993
453  

但我的输出是:

##########################################################################

Author: Ruby On Rails
Book: 2004
Year of Publication: 100
No of Copies: 0

##########################################################################
##########################################################################

Author: Learn Python Easy Way
Book: 1967
Year of Publication: 100
No of Copies: 0

##########################################################################
##########################################################################

Author: Harry Potter and the Sorcerer's stone
Book: 2012
Year of Publication: 150
No of Copies: 0

##########################################################################
##########################################################################

Author: Harry Potter and the Sorcerer's stone
Book: 3045
Year of Publication: 140
No of Copies: 0

##########################################################################
##########################################################################

Author: Harry Potter and the Sorcerer's stone
Book: 2013
Year of Publication: 150
No of Copies: 0

##########################################################################
##########################################################################

Author: Inferno
Book: 1993
Year of Publication: 453
No of Copies: 0

##########################################################################

为什么会跳过第一行?这段代码有什么问题?

【问题讨论】:

  • “fscanf 没有正确扫描值” - 确实如此。正如我已经建议的那样,您最好使用fgets()。远离scanf(),它邪恶且不直观。
  • 调用fgets()后,解析sscanf()的行。您的fscanf() 行扫描fgets() 检索到的行之后的行。

标签: c scanf


【解决方案1】:
fgets ( line, sizeof line, file)

此代码跳过第一行。去掉就好了。

顺便说一句,fscanf 格式字符串"%[^\n]s" 不好。将其替换为"%[^\n]\n"(或更好的"%[^\n]%*c" - 请参阅@chux 的评论)以正确阅读行。或者将 fscanf 替换为 fgets 以在没有这些时髦格式字符串的情况下执行相同的操作。

【讨论】:

  • 注意:"%[^\n]\n""%[^\n] " 没有什么不同。通过在格式字符串中放置\n 或` `,将使用任意数量的空白字符,包括从下一个 行开始的任何空白字符。替代格式包括"%[^\n]*%c"
【解决方案2】:

有类似的问题。虽然这不是一个答案,但它会解决你的问题。

保持输入文件的第一行为空,一切正常。原因由@anatolyg 提及。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-24
    • 2012-07-14
    • 2016-05-02
    • 2016-05-04
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多