【问题标题】:code crashes after scanning/printing first line [closed]扫描/打印第一行后代码崩溃[关闭]
【发布时间】:2017-02-19 17:21:33
【问题描述】:

此代码在扫描并打印数据库的第一行后崩溃。我真的找不到任何解决方案。

坠机照片:

数据库内容:

Matthew Summers 53901523 256325 135500
Jacob Sutherland 52392302 723232.2 1200000
Michael Phelps 58238211 971000.52 653350
Aaron Gordon 59923325 325700.92 623320
Vasil Maglaperidze 59952323 189900.32 330000
Avtandil Shoshiashvili 95234322 432000.72 723023
Michael Jordan 35252372 120899.75 50000
Daniel Whiteman 85238202 178500.53 349800
James Oneal 98773235 90750.23 197050
Haytheim Russels 19326233 178250.22 221580

我的代码:

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

#define CHAR_BUF 128
#define DATA_FILE "database.txt"

typedef struct client
{
    char fname[CHAR_BUF];
    char lname[CHAR_BUF];
    int pnumber;
    float wins;
    float loses;
    float ratio;
}client;

int ReadData(FILE *fp);

int main()
{
    //int lines=0;
    //client client[i];
    FILE *fp = fopen(DATA_FILE, "r"); // opens file
    if(fp==NULL) // checks if .txt file is empty
    {
        printf("Database is empty.");
        exit(1);
    }
    ReadData(fp); // Calls function to read db
    //lines = ReadData(fp);
    //printf("Line amount: %d", lines);
}


/* This function reads data from database
* and assigns values to their variables
*/
int ReadData(FILE *fp)
{
    int i=0;
    client client[i];
    while(!feof(fp))
    {
        fscanf(fp, "%s %s %d %f %f", client[i].fname, client[i].lname,
        &client[i].pnumber, &client[i].wins, &client[i].loses);
        printf("%s %s %d %.2f %.2f\n", client[i].fname, client[i].lname,
        client[i].pnumber, client[i].wins, client[i].loses);
        i++;
    }
    return i;
}

【问题讨论】:

  • 调试器是解决此类问题的正确工具。 询问 Stack Overflow 之前,您应该逐行浏览您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题,以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
  • 为什么这个标签是 C++?
  • client client[i];client client[0];
  • 请记住,作为经验法则,我们更喜欢文字而不是图像。
  • while (!feof(file)) is always wrong,而您的示例用法正是教科书错误时的案例。

标签: c printing crash crash-reports


【解决方案1】:
  1. int i=0;   
    client client[i];  
    

    这将创建一个大小为 0 的数组(这没有任何意义。)
    改成:

    #define MAX 30
    
    client client[MAX];
    
  2. while(!feof(fp))  
    

    当从文件中读取最后一条记录时,末尾仍有\n。所以当这个 while 表达式被评估时 feof(fp) 返回 FALSE 因为它还不是文件的结尾。
    但在下一行 fscanf(fp, "%s %s %d %f %f", client[i].fname, client[i].lname, &amp;client[i].pnumber, &amp;client[i].wins, &amp;client[i].loses); 它是 eofscanf 失败。
    这解释了最后一条垃圾线。
    解决方案:
    检查scanf 是否成功,如果成功则只执行printf

    while(fscanf(fp, "%127s %127s %d %f %f", client[i].fname, client[i].lname, &client[i].pnumber, &client[i].wins, &client[i].loses) == 5)  
    {    
        printf("%s %s %d %.2f %.2f\n", client[i].fname, client[i].lname, client[i].pnumber, client[i].wins, client[i].loses);
        i++;
    }
    

【讨论】:

  • 非常感谢。这是完美的。 ojaxis tkvnis survilebit, 莱万
猜你喜欢
  • 1970-01-01
  • 2013-04-19
  • 2013-04-26
  • 2012-04-15
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多