简体   繁体   English

fgets()在C中将最后一行打印两次

[英]fgets() printing the last line twice in C

I was supposed to open a file from a command-line argument and print the values in it. 我应该从命令行参数打开文件并在其中打印值。 This is my code: 这是我的代码:

#include <stdio.h>
int main(int argc, char* argv[])
{
  float num;
  char const* const filename=argv[1];
  FILE* file=fopen(filename,"r");
  char line[256];
  int j=0;

  while(fgets(line,sizeof(line),file)){

    for( j=0; j<2; j++ )
    {
      if(j == 0)
      {
           fscanf(file, "%f", &num);
           printf("%f \t", num);
      }
      else if(j == 1)
      {
          fscanf(file, "%f", &num);
          printf("%f \n", num);
      }
    }
  }
  fclose(file);
}

This is what I'd like to get as output: 这就是我想要得到的输出:

1 1
2 2
3 3

This is what I'm actually getting: 这是我实际上得到的:

1 1
2 2
3 3
3 3

I don't get what's happening here. 我不明白这里发生了什么。

I done two changes in your code. 我对您的代码做了两次更改。

change 更改

fscanf(file,"%f",&num);

into

sscanf(line,"%f",&num);// here

you are reading the input in loop, but you are getting the value from the file pointer. 您正在循环读取输入,但是您正在从文件指针获取值。 so first line will be skipped. 因此第一行将被跳过。 Then make the test case in while opening the file stream. 然后在打开文件流的同时放入测试用例。

if ( file == NULL)  { 
     perror("fopen");
     return;      
}

Try this code, I done the above changes only, 试试这个代码,我只做了以上更改,

 #include <stdio.h>
 int main(int argc, char* argv[])
 {
    float num;
    char const* const filename=argv[1];
    FILE* file=fopen(filename,"r");
    if ( file == NULL)  { 
       perror("fopen");
      return;      
    }
    char line[256];
    int j=0;
    while(fgets(line,sizeof(line),file) != NULL){
            for(j=0; j<2;j++)
            {
                    if(j==0)
                    {
                            sscanf(line,"%f",&num);
                            printf("%f \t",num);
                    }
                    if(j==1)
                    {
                            sscanf(line,"%f",&num);
                            printf("%f \n",num);
                    }
            }
    }
    fclose(file);
 } 

output: 输出:

 130.000000     130.000000
 210.000000     210.000000
 650.000000     650.000000
 324.000000     324.000000

Your issue is that you read the line with fgets then begin reading on the next line with fscanf . 您的问题是先阅读fgets的行,然后再阅读fscanf的下一行。 In other words after fgets (line, LMAX-1, file) The file position indicator is at the end of line 1. You then fscanf(file, "%f", &num); 换句话说,在fgets (line, LMAX-1, file) ,文件位置指示符位于第1行的末尾 。然后, fscanf(file, "%f", &num); (which skips the '\\n' at the end of line 1 and read the value for line 2). (跳过第1行末尾的'\\n'并读取第2行的值)。 You want: 你要:

while (fgets (line, LMAX-1, file)) {

    for (j = 0; j < 2; j++) {
        if (j == 0) {
            sscanf (line, "%f", &num);
            printf ("%f \t", num);
        } else if (j == 1) {
            sscanf (line, "%f", &num);
            printf ("%f \n", num);
        }
    }
}

Then output becomes: 然后输出变为:

./bin/dbllast dat/dbllast.txt
1.000000        1.000000
2.000000        2.000000
3.000000        3.000000

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

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