[OCLUG-devel] fgets issues

James Colannino james at colannino.org
Mon May 30 17:59:40 PDT 2005


Hey everyone.  I have the following problem with fgets:

I have a string variable that I declare as the following:

char string[50];

I then open a file for reading using fopen, and I pass the pointer to
the file to another function called readstring().  Here's the function:

char * readstring(FILE *file) {

    int i;

    char *string_ptr = string;

    /* string is a globally defined variable; I wouldn't do that in
serious code unless there was no way around it */

    fgets(string_ptr, sizeof(string), file);

    if (string_ptr == NULL)
        return NULL;

    for (i = 0; (i - 1) != '\n'; i++) {
        if (string[i] == '\n')
            string[i] = '\0';
        else
            continue;
    }

    return string_ptr;
}

I then, in the main() function, call that function and then use printf()
to print the line read to the screen.  The man page of fgets tells me
that when fgets encounters EOF, it returns NULL.  Acting on this
assumption, I used the following loop to print every part of a file to
the screen:

char *string_ptr;

while (string_ptr != NULL) {
    string_ptr = readstring(file);
    printf ("%s\n", string_ptr);
}

In theory, string_ptr should be NULL after it's reached the end of the
file, but what happens is that fgets just continuously reads the line
before EOF over and over again.  Up until that point, it works, and
everytime I call fgets it moves further down the file.  However, at the
end, I get caught in an endless loop that prints the last line of the
file to the screen over and over again.

Just in case this makes a difference, I'm reading a file edited by nano
in which an empty line (lacking a '\n' character) is placed below the
last ordinary line of text.

This has me stumped :(  Am I missing something that should be obvious? 
Thanks in advance.

James

P.S. If anyone's interested, here's the entire program:

#include <stdio.h>

char * readstring(FILE *file);

char string[80];

int main(void) {

    int i;   /* used for the for loop */

    FILE *file;   /* the file I'm opening */
    file = fopen("sample.items", "r");
        if (file == NULL) {
            printf ("error opening file.\n");
            return -1;
        }

    char *string_ptr;   /* pointer returned by readstring() */

    /* loop until the pointer returned by readstring() is NULL */

    while (string_ptr != NULL) {
        string_ptr = readstring(file);
        printf ("%s\n", string_ptr);
    }

    return 0;
}


char * readstring(FILE *file) {

    int i;   /* used by the for loop */

    /* create a pointer to the globally defined string variable */
    char *string_ptr = string;

    fgets(string_ptr, sizeof(string), file);

    if (string_ptr == NULL)
        return NULL;

    /* this strips the '\n' character from the string */

    for (i = 0; (i - 1) != '\n'; i++) {
        if (string[i] == '\n')
            string[i] = '\0';
        else
            continue;
    }

    return string_ptr;
}

-- 
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

"A well regulated militia being necessary to the security of a free
state, THE RIGHT of the people to keep and bear arms SHALL NOT BE
INFRINGED." --United States Constitution, Second Ammendment



More information about the OCLUG-devel mailing list