a program to count the number of
occurrences of words in a passage.
The program will read white-space separated
words from a file, convert all words to a
single case, and remove any non-alphanumeric
characters from both ends of the words. It is
to count the number of each word, and
then write to a file a list of word/count pairs, sorted by word.

I am stuck at read from file and store it to var.
it work with user input.

#include<stdio.h>
#include<conio.h>
#include<string.h>
char str[200];
/*char* loadfile(const char *filename)
{
    char *buffer;
    buffer = malloc(size + 1);
    fseek(fp, 0, SEEK_SET);
    fread(buffer, 1, size, fp);
    buffer[size] = '\0';
    return buffer;
}*/

void main()
{
    void opx(char *);
    char str1[20];
    int x = 0, y = 0, i;
//  FILE *fp;
    //fp = fopen(filename, "r");
    /*FILE *inputf;
    inputf = fopen("text.txt", "r");
    if (inputf == NULL)
    {
        printf("cant open it");
    }

    while (inputf != EOF)
    {
    fscanf(inputf, "%s",str1);
    }
    printf("The contents of %s file are :\n", str1);
    if (str1[0] != '\0')
    {
    opx(str1);
    } */
    printf("enter the string");
    gets(str);
    //str1 = loadfile("myfile.txt");
    while (str[y] != 0 && y<strlen(str))
    {
        for (i = 0; str[i] != '\0'; i++)
        {
            str1[i] = '\0';
        }
        while (str[y] != ' '&& y<strlen(str) && str[y] != '\0')
        {
            str1[x] = str[y];
            x++;
            y++;
        }
        if (str1[0] != '\0')
        {
            opx(str1);
        }
        x = 0;
        y++;
    }
    getch();
}
void opx(char *word)
{
    char s;
    size_t i, j, count, c, h;
    i = j = 0;
    count = 0;
    c = strlen(word);
    while (str[i] != '\0' && i<strlen(str))
    {
        h = i;
        while (str[i] != ' '&& i<strlen(str) && str[i] != '?'&& str[i] != '.'&& str[i] != ',')
        {
            if (j == c && str[i] != ' '&& str[i] != '?' && str[i] != '.' && str[i] != ',')
            {
                j++;
            }
            if (str[i] == word[j] && str[i] != ' '&& str[i] != '?' && str[i] != '.' && str[i] != ',')
                j++;
            i++;
        }
        if (c == j)
        {
            count++;
            while (str[h] != ' ' && str[h] != '\0')
            {
                if (str[h] == '\0')
                {
                    str[h] = '\0';
                }
                else
                    str[h] = ' ';
                h++;
            }
        }
        j = 0;
        i++;
    }
    printf("\n%s word occurs %d times", word, count);
}

What type of error do you get when running the commented code?
Here is an example of how to use fscan() http://www.tutorialspoint.com/c_standard_library/c_function_fscanf.htm

For what I can see:

inputf = fopen("text.txt", "r");
if (inputf == NULL)
{
    printf("cant open it");
}
while (inputf != EOF)
{
    fscanf(inputf, "%s",str1);
} // when exiting this loop str1 = EOF, so you lost all read data

Also, could you add some comments to explain what you are doing on each function?

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.