Thread: sorting a simple text file

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    sorting a simple text file

    I have a text file thats looks like this:
    JWF 29
    NTK 659
    QCU 459
    OHK 945
    QLI 345
    I want a program that will sort it by number highest to lowest by number to look like this:
    JWF 29
    QLI 345
    QCU 459
    NTK 659
    OHK 945
    The things that are constant are that the list will always be 5 items long, the characters will always be 3 characters long, and the input file will always be input.txt

    I wrote this program that will read in all the elements in the text file and write them to another file:
    Code:
    # include <stdio.h>
    
    int main(void)
    {
        int score[5][5];
        int x;
        int z;
      
        FILE *outputfile;
        FILE *inputfile;
        
        outputfile = fopen("output.txt","w");
        inputfile = fopen("input.txt","r");
        
        if(inputfile==NULL)
        {
             printf("Error: can't find file 'input.txt' ");
             system("pause");
             return 1;
        }
        
       for(x=0;x<=4;x++)
       {
                for(z=0;z<=2;z++)
                {
                      fscanf(inputfile, " &#37;c", &score[x][z]);
                }
                
                fscanf(inputfile, "%d", &score[x][4]);
        }
        
        for(x=0;x<=4;x++)
        {
              for(z=0;z<=2;z++)
              {
                  fprintf(outputfile,"%c",score[x][z]);
              }
              fprintf(outputfile," %d \n",score[x][4]);
        }
        
        return(0);
    }
    Somewhere in the middle I need to sort it by the numbers in score[0..4][4]

    I'm so lost, because all of the columns must retain the same structure, while only moving positions as a whole.
    Last edited by napalm; 03-25-2008 at 02:24 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you don't want to swap the data around (and who does?) then you'll need to make an "extra" set of indices and sort those instead. That is, you might have an array order_to_print[5] -- order_to_print[0] would tell you which row to print first, order_to_print[1] would tell you which row to print second, etc.

    Edit to add: and while your method of a 5x5 array of integers is ... correct ... it might be a good idea to look at structures (which will allow you to group characters and integers together into one "thing").
    Last edited by tabstop; 03-25-2008 at 02:29 PM.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    ^ good idea

    I'll try that and get back to you

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by napalm View Post
    I have a text file thats looks like this:

    I want a program that will sort it by number highest to lowest by number to look like this:
    Use your terminal. It probably has a sort utility that you can use.
    Code:
    C:\Documents and Settings\JCK>notepad in.txt
    
    C:\Documents and Settings\JCK>sort in.txt /+4
    JWF 29
    QLI 345
    QCU 459
    NTK 659
    OHK 945
    
    C:\Documents and Settings\JCK>

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    ^ This program is going to be later turned into a function to be used in a larger program, but thanks anyways. I never knew you could do that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Replies: 1
    Last Post: 04-24-2005, 03:45 AM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. Sorting text file problem...
    By John-m in forum C Programming
    Replies: 3
    Last Post: 10-01-2002, 04:51 PM