-2

This code crashes after scanning and printing first line from database. I really couldn't find any solution to this.

A shot of the crash:

Crash

Contents of the database:

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

My code:

#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;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Feb 19 '17 at 17:22
  • 1
    Why is this tagged C++? – Biffen Feb 19 '17 at 17:23
  • 1
    `client client[i];` is `client client[0];` – BLUEPIXY Feb 19 '17 at 17:23
  • 1
    Bear in mind we prefer text over images as a rule of thumb. – cadaniluk Feb 19 '17 at 17:25
  • I am just a beginner and I don't know how to debug properly that's the reason I am asking your help. If anyone willing to help, please reply – Levan Kalandadze Feb 19 '17 at 17:30
  • 1
    [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong), and your example usage is exactly a text book case of when it is wrong. – Jonathan Leffler Feb 19 '17 at 17:52
  • Also Check `FLT_DIG` in ``. – BLUEPIXY Feb 19 '17 at 18:00

1 Answers1

0
  1. int i=0;   
    client client[i];  
    

    This will create an array of size 0 (which doesn't make any sense.)
    Change it to:

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

    When the last record has been read from the file there is still \n at the end. So when this while expression gets evaluated feof(fp) return FALSE as it's not the end of file yet.
    But in the next line fscanf(fp, "%s %s %d %f %f", client[i].fname, client[i].lname, &client[i].pnumber, &client[i].wins, &client[i].loses); it's eof and scanf fails.
    Which explains the last garbage line.
    Solution :
    Check whether scanf succeeded or not and if it did then only execute 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++;
    }
    
rootkea
  • 1,474
  • 2
  • 12
  • 32