0

I am new to c programming. I am working of a text file with the following format

1 2009 james smith 2 18

2 2010 bob davies 5 18

3 2010 Allan Thomson 15 26

4 2010 Brad Haye 15 26

What I want to do is read each line and print to the console in meaningful way like

James smith made his debut in 2009 and earns 2m a year. He has earned 18m over his lifetime with an average of xyzM a year

Then

Highest Earner: ?
Average Earning: ?
total players:

This is what I have so far:

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

typedef char *string;

int main() {

    int i = 0, line = 5;
    char ch[100];
    string array[4];

    FILE *myfile;
    myfile = fopen("C:/Data/Players.txt","r");
    if (myfile== NULL)
    {
        printf("File Does Not Exist \n");
        return 1;
    }

    while(line--)
    {
        fscanf(myfile,"%s",&ch[i]);
        printf("\n%s", &ch[i]);
        array[0] = array[i];
        i++;

        if(i=5)
        {
            printf("Yes");
        }


    }

    fclose(myfile);

    return 0;
}
Sandeep
  • 155
  • 1
  • 14
kayze
  • 738
  • 8
  • 19
  • 33

3 Answers3

0

You should read the file line by line, parsing the strings, until you encounter the end of file (EOF). You only know you encountered it AFTER you have already read it.

This is my solution:

#include <stdio.h>

int main()
{
    FILE *file;
    int line_number;
    int year;
    char fname[10];
    char lname[10];
    int m_a_year;
    int m_over_lifetime;

file = fopen("textfile.txt", "r");

while(!feof(file))
{
    fscanf(file, "%d %d %s %s %d %d", &line_number, &year, fname, lname, &m_a_year, &m_over_lifetime);
    printf("%s %s made his debut in %d and earns %dm a year. He has earned %dm over his lifetime with an average of %dM a year\n\n", fname, lname, year, m_a_year, m_over_lifetime, (m_over_lifetime/(2016-year)));
}

fclose(file);

    return 0;
}

Output:

james smith made his debut in 2009 and earns 2m a year. He has earned 18m over h
is lifetime with an average of 2M a year

bob davies made his debut in 2010 and earns 5m a year. He has earned 18m over hi
s lifetime with an average of 3M a year

Allan Thomson made his debut in 2010 and earns 15m a year. He has earned 26m ove
r his lifetime with an average of 4M a year

Brad Haye made his debut in 2010 and earns 15m a year. He has earned 26m over hi
s lifetime with an average of 4M a year
W2a
  • 736
  • 2
  • 9
  • 23
  • 2
    [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – BLUEPIXY Sep 10 '16 at 11:53
  • feof(file) is only True after you read the EOF byte, which is at the END OF the FILE. @BLUEPIXY – W2a Sep 10 '16 at 12:26
  • 2
    The idea is to rewrite the loop as `while( fscanf(...) == 6 ) { printf(...); }`. Doing so you are checking both EOF and that all the fields are read. – Bob__ Sep 10 '16 at 12:52
0

Please try this code that does almost everything you need.

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

int main(void) {
    int status;
    int number;
    FILE *fp;
    if ((fp = fopen("C:/Data/Players.txt", "r+")) == NULL) {
        printf("No such file\n");
        exit(1);
    }
    int x, y;
    char firstname[10];
    char lastname[20];
     while (fscanf(fp, "%d %d %s %s %d %d", &number, &x, firstname, lastname, &y, &status) == 6) {
        printf("%s %s made his debut in %d and earns %dm a year. He has earned %dm over his lifetime with an average of %dM a year\n\n", firstname, lastname, x, y, status, (status/(2016-x)));
    }
    return 0;
}

output

james smith made his debut in 2009 and earns 2m a year. He has earned 18m over his lifetime with an average of 2M a year

bob davies made his debut in 2010 and earns 5m a year. He has earned 18m over his lifetime with an average of 3M a year

Allan Thomson made his debut in 2010 and earns 15m a year. He has earned 26m over his lifetime with an average of 4M a year

Brad Haye made his debut in 2010 and earns 15m a year. He has earned 26m over his lifetime with an average of 4M a year
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
0

you can make it way more simpler. First you don't need the variable "Line", when c reads a file he can tell if he reaches the end of it, also we don't use a string type in c, we use a char array, but in your code the string array useless, it's just there taking space, as for the while the comments above are correct and can give you an idea on how to read a file

E.Othmane
  • 1
  • 1
  • welcome to stackOverflow. While your advice here might be correct, consider adding the code that you think will work. This way, it may help future readers. Also include the motivations for why you made changes to the OPs code. – lmo Sep 10 '16 at 12:56