undone98
undone98

Reputation: 77

Program to read words from a file and count their occurrence in the file

I'm currently trying to make a program that will read a file find each unique word and count the number of times that word appears in the file. What I have currently ask the user for a word and searches the file for the number of times that word appears. However I need the program to read the file by itself instead of asking the user for an individual word.

This is what I have currently:

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

int main(int argc, char const *argv[])
{   
int num =0;
char word[2000];
char *string;

FILE *in_file = fopen("words.txt", "r");

if (in_file == NULL)
{
    printf("Error file missing\n");
    exit(-1);
}

scanf("%s",word);

printf("%s\n", word);

while(!feof(in_file))//this loop searches the for the current word
{
    fscanf(in_file,"%s",string);
    if(!strcmp(string,word))//if match found increment num
    num++;
}
printf("we found the word %s in the file %d times\n",word,num );
return 0;
}

I just need some help figuring out how to read the file for unique words (words it hasn't checked for yet) although any other suggestions for my program will be appreciated.

Upvotes: 2

Views: 59629

Answers (3)

Chandra Mani
Chandra Mani

Reputation: 1

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

int main(int argc, char*argv[])
{   
int num =0;
char word[2000];
char string[30];

FILE *in_file = fopen(argv[1], "r");

if (in_file == NULL)
{
    printf("Error file missing\n");
    exit(-1);
}

scanf("%s",word);

printf("%s\n", word);

while(!feof(in_file))//this loop searches the for the current word
{
    fscanf(in_file,"%s",string);
    if(!strcmp(string,word))//if match found increment num
    num++;
}
printf("we found the word %s in the file %d times\n",word,num );
return 0;
}`

if any suggestion plz..most welcome

Blockquote

Upvotes: 0

anatolyg
anatolyg

Reputation: 28300

You have to split your code into functions (subroutines).

One function would read the file and record all words; the other would count the number of occurrences for each word.

int main(int argc, char const *argv[])
{
    char *words[2000];

    // Read the file; store all words in the list
    int number_of_words = ReadWords("words.txt", words, 2000);

    // Now count and print the number of occurrences for each word
    for (int i = 0; i < number_of_words; i++)
    {
        int n = CountOccurrences(words[i], "words.txt");
        printf("we found the word %s in the file %d times\n", words[i], n);
    }

    // Deallocate dynamically allocated memory
    Cleanup(words, number_of_words);
}

Note how the main function is relatively short. All the details are in the functions ReadWords and CountOccurrences.

To implement reading all words from a file:

int ReadWords(const char *filename, char *words[], int max_number_of_words)
{
    FILE *f = fopen(filename, "rt"); // checking for NULL is boring; i omit it
    int i;
    char temp[100]; // assuming the words cannot be too long

    for (i = 0; i < max_number_of_words; ++i)
    {
        // Read a word from the file
        if (fscanf(f, "%s", temp) != 1)
            break;
        // note: "!=1" checks for end-of-file; using feof for that is usually a bug

        // Allocate memory for the word, because temp is too temporary
        words[i] = strdup(temp);
    }
    fclose(f);

    // The result of this function is the number of words in the file
    return i;
}

Upvotes: 1

md5
md5

Reputation: 23737

If you want to print every line contained in the file just once, you have to save the strings you have read in a given data structure. For example, a sorted array could do the trick. The code might look as follow:

#include <stddef.h>

size_t numberOfLine = getNumberOfLine (file);
char **previousStrings = allocArray (numberOfLine, maxStringSize);
size_t i;

for (i = 0; i < numberOfLine; i++)
{
    char *currentString = readNextLine (file);

    if (!containString (previousStrings, currentString))
    {
        printString (currentString);
        insertString (previousStrings, currentString);
    }
}

You may use binary search to code the functions containString and insertString in an efficient way. See here for further informations.

Upvotes: 1

Related Questions