-1

So guys I have a file with various lines such as "GAME: 1 Bob (@) - 404" the name of the player is Bob and his highest score is 404 and there are many lines like this. Basically I have to read each line and see who has the highest score and print his name and score out in c. So far I have this:

  FILE *fp;
  char * line = NULL;
  size_t len = 0;
  size_t read;

  fp - fopen("Leaderboard.dat", "r");
  if (fp == NULL) {
  printf("File empty/unable to open");
  }

now im actually not sure how to do this properly..

  • 1
    This code has undefined behavior. `fp - fopen( ... );` is wrong. Also, what have you tried?. Reading a file line by line is a very common task, just google it. You will find a million examples. – Iharob Al Asimi Aug 09 '16 at 01:52
  • i cant seem to figure it out, like i can fetch the line but i cant seem to compare which line contains the highest score and print that line out – jass preet Aug 09 '16 at 01:56
  • 1
    Possible duplicate of [C read file line by line](http://stackoverflow.com/questions/3501338/c-read-file-line-by-line). And what exactly do you think subtracting `fopen()` from an uninitialized `FILE *fp` is going to do? – Ken White Aug 09 '16 at 01:57
  • So your real question is, "*how to find a substring in a text line?*", is it? – Iharob Al Asimi Aug 09 '16 at 01:57
  • 1
    This is not an [mcve]. What error message are you getting? What did you learn when running it under debugger? Could you at least try and add lines that read the file and possibly store the name and score of the highest scorer as you read each line? it's just an `if score < whateverwasread score=whateverwasread;` inside a loop `while (fgets(line, maxline, fp) != NULL)`. – alvits Aug 09 '16 at 01:59
  • 2
    @iharob - I think the real question is: Can you help me write a code cause I have no idea where to start. – alvits Aug 09 '16 at 02:00
  • @jasspreet - if you can't figure out how to parse a line, or read a line, then maybe your instructor wasn't teaching the language effectively. Time to find a new instructor. – alvits Aug 09 '16 at 02:02
  • I searched, and there is not a single question mark in your "question". – barak manos Aug 09 '16 at 03:00

2 Answers2

0
char buffer[100];
char code[4];
char name[100];
int maxScore=0;
char maxName[100];
char maxCode[4];

while(fscanf(fp,"%s %s %s %c %d",buffer,name,code,buffer[0],&score) !=EOF)
{
    if(score>maxScore)
    {
        maxScore=score;
        strcpy(maxCode,code);
        strcpy(maxName,name);
    }
}
printf("Name: %s. Code %s Score %d",maxName,maxCode,maxScore);
Dr.Haimovitz
  • 1,568
  • 12
  • 16
  • It would be better to check that `fscanf` returned the number of elements requested (in this case 5), since `fscanf` may return a number fewer than that (for example, if not the `%d` fails to match an integer etc). Also, `buffer[0]` is of type `char` but `%c` expects `char *`. Lastly, the `%s` specifiers are unbounded, so there is the potential for buffer overflow. – dreamlax Aug 09 '16 at 05:28
-1

This code should do the trick, Just note it has not been checked or compiled.

   FILE *fp;

   fp = fopen("Leaderboard.dat", "r");

   if (fp == NULL) {
       printf("File empty/unable to open");
   }

    char buffer[100];
    char name[100];
    char winnerName[100];
    char code;
   char winnerCode;
    int maxScore = 0;

    while(fgets(buffer,1000,fp)!=NULL)
    {
        // getting the points from the end of the line
        int last = strlen(buffer) -1;
        while(isdigit(buffer[last]))
        {
            last--;
        }
        int score = atoi(buffer+last+1);

       //getting the name from the start of the line
        int i = 0;
        while(!isdigit(buffer[i]))
        {
            i++;
        }
        i ++;
        int k=0;
        while(!isdigit(buffer[i]))
       {
           if(isalpha(buffer[i]))
           {
                name[k++] = buffer[i];
            }
           if(buffer[i] == '(' )
           {
               code= buffer[i+1];
           }
           i++;
        }
        name[k] = '\0';
        // is greater then max
        if(score > maxScore)
        {
             strcpy(winnerName,name);
             maxScore = score;            
             winnerCode=code;
        }
    }

   printf("Name: %s , Code: %c, Score: %d",winnerName,winnerCode,maxScore);
Dr.Haimovitz
  • 1,568
  • 12
  • 16