Search code examples
cfiledebuggingfgets

why in this piece of code, fgets returns null when it shouldn't?


I am coding a program that a piece of it is as below:

#define BUFFSIZE 2000
while( fgets(buff, BUFFSIZE - 1, fPtr) != NULL)
{
    //how many words is read from the source file in the current line
    int wordread = 0;

    word = strtok(buff, punc);
    //one word read
    while( word != NULL)
    {
        //sorting
        int k = 0;
        while(strcmp(word, words[k]) < 0)
            k++;
        if(strcmp(word, words[k]))
        {
            last++;
            for(int l = last; l >= 0; l--)
                strcpy(words[l + 1],words[l]);
            strcpy(words[wordread],word);
        }
        index[last][lineread]++;

        wordread++;//go to next word

        word = strtok(NULL, punc);
    }
    lineread++;//go to next line
}

but I understood that the compiler doesn't go inside the first while

while( fgets(buff, BUFFSIZE - 1, fPtr) != NULL)

I understood that this fgets returns NULL at first. but fPtr has an correct address and in the address there is a file of some data.

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

and the definition of buff is:

char buff[BUFFSIZE];

can anyone help? if the whole of the code is needed, tell me to write all of it.


Solution

  • The problem that the compiler didn't get into the first loop was the the file that it was supposed to read of, wasn't there. and It was returning NULL