Thread: Looking for direction on how to order scores then write all entries to a file in C

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    10

    Looking for direction on how to order scores then write all entries to a file in C

    I have a program that is operating now, it pulls scores from a text file. Which I want it to the scores are from the MLB AL Central Division it then multiplies wins by 2 points and assigns a score to the leader and prints all info to the screen. I need to have it put in order and then saved to a new txt file. Right now I have it saving the final team without being sorted just looking for a nudge in the right direction to achieve my goal. Thanks

    Code:
    
    
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include<string.h>
    int main(void)
    
    {
    struct records {
         char filename;
         char team[50];
         int wins;
         int tie;
         int loss;
         int points;
     };
    struct records roster;
         FILE*ifp = NULL;
         FILE*afd = NULL;
         constint argv;
         char filename2[64]={0};
         char filename[64]={0};
         int points;
         int points2;
         int total;
    
    
         printf("\nPlease enter the the .txt file you would like to open: ");
         scanf("%63s", filename);
         printf("Opened file name %s",filename);
         ifp = fopen(filename,"r");
    
    
                if(ifp == NULL)
         {
                printf("Could not open");
                printf("\nPlease enter the the .txt file you would like to open: 
                ");
                scanf("%63s", filename);
                printf("Opened file name %s",filename);
                ifp = fopen(filename,"r");
         }
    
         {
            printf("\nReading the file %s \n", ifp);
            while(fscanf(ifp,"%s %d %d %d", roster.team,&roster.wins, 
            &roster.loss,&roster.tie)!= EOF)
            printf("%s Wins:%d Losses:%d ties:%d total league points:%d\n", 
           roster.team, roster.wins, roster.loss, roster.tie,(roster.wins *2+ 
           roster.tie *1), total);
         }
            printf("closing %s", filename);
            fclose(ifp);
    
            printf("\nPlease enter the the .txt file you would like to write to: 
            ");
            scanf("%63s", filename2);
            printf("Opened file name %s",filename2);
            afd = fopen(filename2,"a+");
    
                if(afd == NULL)
        {
                printf("Could not open");
                printf("\nPlease enter the the .txt file you would like to open: 
                ");
                scanf("%63s", filename2);
                printf("Opened file name %s",filename2);
                afd = fopen(filename2,"a+");
          }
            points = roster.wins *2;
            points2 = roster.tie *1; 
            total = points + points2;   
            afd = fopen(filename2,"a+");
            fprintf(afd,"%s %d %d %d %d\n", roster.team, roster.wins, 
            roster.loss, roster.tie, total);
            printf("\nThanksfor your data 
            entry!\nClosing%s", filename2);
            fclose(afd);
    
            return0;
        }
    
    

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,685
    Your indentation is awful. It's not helped by copy/pasting all the font/colour tags from your not-so-smart IDE.
    Use either 'copy-as-text' in your IDE, or 'paste-as-text' in your browser.

    You have braces in some strange places.

    Your 'roster' seems to contain only one team. If you're looking to sort things, you need an array.

    Your 'struct records' is strangely plural, yet it only contains a single team.
    Similarly, 'wins' is plural, but 'tie' and 'loss' are singular.
    The 'char filename' is unused.

    The way to sort things is to use qsort() in stdlib.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-25-2017, 12:57 PM
  2. How to open .csv file and read the entries using C
    By Archimedes79 in forum C Programming
    Replies: 5
    Last Post: 05-22-2017, 09:50 PM
  3. how to print scores in descending order
    By ingeniousreader in forum C Programming
    Replies: 5
    Last Post: 03-05-2012, 06:52 PM
  4. Replies: 1
    Last Post: 12-04-2011, 11:31 PM
  5. Write the low-order 8 bits of the time to the disk file
    By kfuller002 in forum C Programming
    Replies: 2
    Last Post: 11-10-2010, 02:32 PM

Tags for this Thread