簡體   English   中英

錯誤 output 將數據從文件讀入程序

[英]Wrong output for reading data from file into program

我一直在研究一個程序,將文件中的數據讀取到程序中並打印出來:

這是我的代碼:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char *p;
    int e;
    float f;
    static char a[50];
    FILE *fp;
    fp = fopen("new_input.txt", "r");
    if (fp == NULL)
        exit(0);
    while (1)
    {
        p = fgets(a, 50, fp);
        if (p == NULL)
            break;
        printf("%s", a);
        printf("\n");
        fscanf(fp, "%d", &e);
        printf("%d", e);
        printf("\n");
        fscanf(fp, "%f", &f);
        printf("%f", f);
        printf("\n");
    }
    fclose(fp);
    return 0;
}

文件 new_input.txt 的內容是:

Girik
12   
19.98   
Nikhil
13
90.89

但是在 OnlineGdb 上運行時,我得到以下 output:

Girik 
     
12 
19.980000   
      
     
12  
19.980000 
khil         
13          
90.889999 

有人可以用我的代碼解釋問題嗎?

我擔心 output 中不完整的名稱“Nikhil”以及兩次打印值 12 和 19.99。

代碼鏈接: https://onlinegdb.com/r1ufoP8-d

出現問題是因為上一個fscanf不解析\n ,所以當循環重新啟動時fgets會解析並打印它,自然下一個fscanf無法解析字符串( "Nikhil" ,未解析),因為它需要一個int ,所以從那里開始走下坡路,因為這些值沒有被解析,所以打印的是前一個周期中ef的舊值。

如果您更改最后一個fscanf以便它解析\n一切正常:

現場演示

while (1)
{
    p = fgets(a, 50, fp);
    if (p == NULL)
        break;
    printf("%s", a);
    fscanf(fp, "%d", &e);
    printf("%d", e);
    printf("\n");
    fscanf(fp, "%f\n", &f); //<--here
    printf("%.2f", f);//.2f specifier so it prints only 2 decimal places
    printf("\n");
}

指針也是不必要的,您可以使用fgets本身作為條件,

while (fgets(a, sizeof a, fp)) //will read until the end of the file
{
    printf("%s", a);
    //... same code
}

無論如何,這是一個您可以使用的版本,它將為您呈現預期的結果和 output,而且我認為更好:

現場演示

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int e;
    float f;
    static char a[50];
    FILE *fp;
    fp = fopen("new_input.txt", "r");
    if (fp == NULL){
        return EXIT_FAILURE;
    }
    // will parse until \n or 49 characters max (given the container size)
    while (fscanf(fp, " %49[^\n]", a) > 0)
    //                 ^ space - will discard any blank characters         
    {
        printf("%s\n", a);
        if(fscanf(fp, "%d", &e) > 0){
            printf("%d\n", e);
        }
        if(fscanf(fp, "%f", &f) > 0){
            printf("%.2f\n", f);
        }
    }
    fclose(fp);
    return EXIT_SUCCESS;
}

我還添加了對fscanf返回的檢查,這始終是一個好習慣。

甚至:

//...
while (fscanf(fp, " %49[^\n]%d%f", a, &e, &f) == 3)
{
    printf("%s\n%d\n%.2f\n", a, e, f);
}
//...

但是,我認為最好的解決方案是使用fgets解析所有內容並使用sscanfstrtol / strtof轉換值,這是一個更強大的選擇。 試試看。

對於“從文件中讀取數據到程序中並打印”,您可以使用例如 fgets 或 fscanf。 這兩種用途都包含在您的示例的以下改編中:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char *p;
    int e;
    float f;
    static char a[50];
    FILE *fp;
    
    // use fgets
    printf("\nVersion with using fgets\n\n");
    fp = fopen("new_input.txt", "r");
    if (fp == NULL) exit(0);
    while(fgets(a, 50, fp)!=NULL)
    {
        printf("%s", a);
    }
    fclose(fp);
    
    // use fscanf
    printf("\nVersion with using fscanf\n\n");
    fp = fopen("new_input.txt", "r");
    if (fp == NULL) exit(0);
    while(fscanf(fp,"%s\n",a)!=EOF)
    {
        printf("%s\n", a);
    }
    fclose(fp);    
    
    return 0;
}

注釋:

  1. a[50] 的大小需要大於最大行大小。
  2. 您還可以 fscanf 查找數字(如果您確定它是一個數字)。
  3. 檢查字符串是否為 integer 或浮點(語言環境)是否為新主題。

另一種選擇:假設您可以依賴“new_input.txt”中的格式(以解釋 Devolus 的評論)。

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char *p;
    int g;
    float f;
    static char a[50];
    FILE *fp;
    
    fp = fopen("new_input.txt", "r");
    if (fp == NULL) exit(0);
    
    while(fscanf(fp,"%s\n",a)!=EOF){
        printf("%s\n", a);
        fscanf(fp,"%d\n",&g);
        printf("%d\n", g);
        fscanf(fp,"%f\n",&f);
        printf("%f\n", f);
    }
    fclose(fp);    
    
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM