1. PF Contest - Win "Conquering the Physics GRE" book! Click Here to Enter
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Sorting array structs in C

  1. Jul 23, 2010 #1
    1. The problem statement, all variables and given/known data

    Develop a C program that to count how many times each word appears in a large text file. Your program must read words from a file and output the number of times each word of the file appears.
    1. Implement an algorithm to count how many times each word appears in a large text file.
    2. Ask the name of the file to count the words
    3. The output of the program is a file. The name of the file must be asked by the program
    4. The output file should be ordered by frequency of the words

    2. Relevant equations


    3. The attempt at a solution

    Code (Text):
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    typedef struct WORD
        char wordname[50];
        int count;

    word_t new_word(char* words)
        word_t new_w;
        strcpy (new_w.wordname, words);
        return new_w;

    int main(int argc, char *argv[])
        FILE *fp;
        FILE* pInput;
        FILE* pOutput;
        char buffer[50];
        char buffer1;
        char filename[50];
        char output[50];
        char array1[1000][30];
        char* tempword;
        int i = 0;
        int j = 0;
        int numcount = 0;
        int i1 = 0;
        int j1 = 0;
        int tempcount;
        int char_count = 0;
        int word_count = 1;
        bool check = false;
        bool inlist;

        printf("Enter the name of the file to open (include the .txt): ");
        printf("Enter the name of the output file (include the .txt): ");

        pInput = fopen(filename, "r");
        pOutput = fopen("temporary.txt","w");
        fp = fopen(output, "w");

        while (buffer1 != EOF)
            buffer1 = fgetc(pInput);
            if (buffer1 != ' ')
                array1[j1][i1] = buffer1;
                fprintf(pOutput, "%c", array1[j1][i1]);
                if (buffer1 != '\n' && buffer1 != '\0')
                if (buffer1 == '\n' || buffer1 == '\0')
                if (buffer1 == ' ')
                i1 = 0;
                if (buffer1 == '\0')
                    fprintf(pOutput, '\0');
                    fprintf(pOutput, "\n");

        fp = fopen("temporary.txt", "r");

        word_t words[word_count];

        for (i = 0; i < word_count; i++)
            fgets (buffer, 50, fp);
            words[i] = new_word(buffer);
            printf("words[%d] is %s", i, words[i].wordname);
        fclose (fp);
        tempcount = word_count;
        for (j = 0; j < tempcount; j++)
            tempword = words[j].wordname;
            for (i = 0; i < word_count; i++)
                if (strcmp(tempword, words[i].wordname) == 0)

            words[j].count = numcount;
            numcount = 0;
        printf ("\nPrinting results...\n\n");

        for (j = 0; j < tempcount; j++)
            for (i = 0; i < j; i++)
                if (strcmp(words[j].wordname, words[i].wordname) == 0)
                    inlist = true;
                    inlist = false;
            if (inlist == false)
                printf ("%d\t%s", words[j].count, words[j].wordname);
                fprintf(fp, "%d\t%s", words[j].count, words[j].wordname);
        return 0;
    I need help with the sorting of the structs, and outputting the sorted structs to a file.

    Here's the sample text file that I've been working with:

    I can read the file, can count the number of times each word appears, but I'm not quite sure how to use the qsort function to sort an array of structs, nor does the output want to go into the specified file. I've looked all over the internet but still can't seem to understand how it works. Can anyone show me how to at least get the sorting done?
  2. jcsd
  3. Jul 24, 2010 #2


    Staff: Mentor

    For the sorting part, qsort() is set up to sort an arbitrary array. One of the parameters is a pointer to a comparison function that you supply. The comparison function will need to decide whether the frequence of the word in words.wordname is "less than" the frequency of the word in words[j].wordname. At this level, all you need to do is compare words.count with words[j].count. The word with the higher frequency should go before the word with lower frequency I would think.

    Once you have the array sorted by frequency, print (using fprintf) the words and their frequencies to the output file.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - Sorting array structs Date
Sorting Arrays to a file Java Apr 22, 2015
Sorting array into ascending order in C++ Mar 2, 2013
Algorithm Complexity: Sorted Arrays into Sorted Array Mar 8, 2012
Sorting Array Dec 2, 2009
Merge sort with array of structs Mar 31, 2009