Search code examples
cfgets

How to use fgets in this code?


I need to count words from a text file. For example the file includes

"Hans Mustermann Ingo Mustermann Trude Musterfrau Trude Mustermann Gerda Muster"

when I search for Trude Mustermann it should show 5 hits.

My code runs only one line I guess and then it stops.

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

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

FILE *in_file = fopen("test.txt", "r"); //reading the words

if (in_file == NULL)
{
    printf("Dataerror\n"); //if word not found
    exit(-1);
}

else {
    fscanf("%s", word);

    while(!feof(in_file))//search for the word

    {
        fscanf(in_file,"%s", string);
        if(!strstr(string , word))//if hit a word
        num++;
    }

    printf( "%d Hit \n" ,num );
}
return 0;
}

I have tried using scanf("%[^\n]",word);getchar(); but that did not help.


Solution

  • That’s was one of my test to learn C, long time ago. As I remember, you need something like the following:

    #define numWords 2
    char * words[numWords] = {"Trude", "Mustermann"};
    int counters[numWords] = {0, 0};
    
    void CountWords(char * Buffer)
    {
        char *p1=Buffer; 
        char *p2;
        char *p3;
        int i;
        int n=0;
    
        // initialize the counters
        memset(counters, 0x00, sizeof(counters));
    
        do
        {
            p2=NULL;
            for (i=0; i < numWords; i++)
            {
                p3=strstr(p1, words[i]);
                if (p3) // found a word 
                {
                    // if p2 is null or p3 is before p2
                    if (!p2 || p3 <= p2) 
                    {
                        p2=p3;
                        n=i;
                    }
                }
            }
            if (!p2) break; // no more words
            counters[n]++;
            p1=p2+strlen(words[n]);
        } while (TRUE);
    
        for (i=0; i < numWords; i++)
        {
            printf("The word '%s' appears %5d time/s\n", words[i], counters[i]); 
        }
    }
    

    To test that function:

    CountWords("Hans Mustermann Ingo Mustermann Trude Musterfrau Trude Mustermann Gerda Muster");
    

    If you want to test from a text file, then use the following:

    // To read from a file, I'd suggets to read the whole file in memory, 
    // and search in that buffer.
    if (_sopen_s(&FD, "test.txt", O_RDONLY | O_BINARY, _SH_DENYNO, _S_IREAD)) 
    {
        return; // file not found
    }
    fSize=_lseek(FD, 0, FILE_END); // get file's size
    _lseek(FD, 0, FILE_BEGIN); // rewind it
    // Allocate memory to read the file's content
    if ((Buffer = (char *)malloc(fSize+1)) == NULL) 
    {
         _close(FD);
         return; // Not enough memory
    }
    
    __try
    {
        i=_read(FD, Buffer, fSize);
        _close(FD);
        if (i != fSize) return; // error reading the file
        Buffer[fSize]=NULL;
        CountWords(Buffer);
    }
    __finally
    {
        free(Buffer);
    }
    

    To test from the file, you need to include the following header files.

    #include <stdio.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    #include <io.h>
    #include <share.h>