简体   繁体   English

fscanf无法正确扫描值

[英]fscanf not scanning values properly

I'm having the following code: 我有以下代码:

#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++;
  }

}  

The input file is: 输入文件为:

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  

But my output is: 但是我的输出是:

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

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

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

Why is it skipping the first line? 为什么跳过第一行? What is wrong with this code? 此代码有什么问题?

fgets ( line, sizeof line, file)

This code skips the first line. 此代码跳过第一行。 Remove it and it will be good. 删除它会很好。

By the way, fscanf format string "%[^\\n]s" is not good. 顺便说一句, fscanf格式字符串"%[^\\n]s"不好。 Replace it by "%[^\\n]\\n" (or better, "%[^\\n]%*c" - see comment by @chux) to read lines properly. 将其替换为"%[^\\n]\\n" (或更好的是"%[^\\n]%*c" -请参阅@chux的注释)以正确读取行。 Or replace fscanf by fgets to do the same without these funky format strings. 或者用fgets替换fscanf ,以在没有这些时髦格式字符串的情况下执行相同的操作。

Had the similat issue. 有类似的问题。 though it' not an answer but it will fix your problem. 尽管这不是一个答案,但可以解决您的问题。

Keep the first line of the input file empty and it will all work fine.Reason is mention by @anatolyg. 保持输入文件的第一行为空,一切都会正常工作。@ anatolyg提到了原因。

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

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