Hey guys im having trouble completing this C program. Its suppose to ask the user to enter a file to read from, read the file, count how many times each word appears and print the results in a different file(also user input) arranged from high occurrence.

Heres what i have so far

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

typedef struct AWORD
{
    char wordname[50];
    int count;
}a_word;

a_word nword(char* words)
{
    a_word new;
    strcpy (new.wordname, words);
    return new;
}

int compare (words[i].wordname, words[j].wordname)
{
     if (words[i].count < words[j].count)
    {
        return -1;
    }
    else if (words[i].count > words[j].count)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int main(int argc, char *argv[])
{
    int i = 0,j = 0,k = 0, m = 0, count = 0,count2 = 0, count3 = 0, wrdcnt = 1,$
    char cur, cur2[50], file1[25], file2[25], list[1000][k], *buffer;
    FILE *pFile, *pFile2;

    printf("Please type the name of the input file:\n ");
    scanf("%s",file1);
    printf("%s",file1);
    pFile = fopen(file1, "r");

    printf("Please type the name of the output file:\n ");
    scanf("%s",file2);
    pFile2 = fopen(file2, "r");

    while (cur != EOF)
    {
        cur = fgetc(pFile);
        if (cur != ' ')
        {
            list[m][k] = cur;
            fprintf(pFile2, "%c", list[m][k]);
            k++;
            if (cur != '\n' && cur != '\0')
            {
                count2++;
            }
            if (cur == '\n' || cur == '\0')
            {
                wrdcnt++;
            }
        }
        else
        {
            if (cur == ' ')
            {
                wrdcnt++;
            }
            m++;
            k = 0;
            if (cur == '\0')
            {
                fprintf(pFile2,"%s", &cur);
            }
            else
            {
                fprintf(pFile2, "\n");
            }
        }
    }
    fclose(pFile);
    fclose(pFile2);

    pFile2 = fopen(file2, "r");          

    a_word words[wrdcnt];

    for (i = 0; i < wrdcnt; i++)
    {
        fgets (cur2, 50, pFile2);
        words[i] = nword(cur2);
        printf("words[%d] is %s", i, words[i].wordname);
    }
    printf("\n");
    count3 = wrdcnt;
    for (j = 0; j < count3; j++)
    {
        buffer = words[j].wordname;
        for (i = 0; i < wrdcnt; i++)
        {
            if (strcmp(buffer, words[i].wordname) == 0)
            {
                count++;
            }
        }

        words[j].count = count;
        count = 0;
    }
    printf ("\nWord Counter Program Starting...");
    printf("\nWord Counter Program Finished. Check %s file\n", file2);

    for (j = 0; j < count3; j++)
    {
        for (i = 0; i < j; i++)
        {
            if (strcmp(words[j].wordname, words[i].wordname) == 0)
            {
                same = 0;
                break;
            }
            else
            {
                same = 1;
            }
        }
        if (same == 1)
        {
            printf ("%d\t%s", words[j].count, words[j].wordname);
            fprintf(pFile2, "%d %s\n", words[j].count, words[j].wordname);

        }

    }    
    fclose(pFile2);
    return 0;
}

I havent sorted yet. we are suppose to use qsort but i dont think my compare function is right. Any help would be greatly appreciated. Thank you.

You would be better off to post that on the DaniWeb C Forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.