Thread: testing for EOF with strings

  1. #1
    Unregistered
    Guest

    testing for EOF with strings

    if the return value of EOF is an int, then how can you test for it with strings? I currently have a pointer to the string and I use it to test chars. When I try setting up a loop to test for the EOF at the end of a text file and compile I get an infinite loop with puts only printing out the last word over and over. what am I doing wrong?

    Thanx
    Doug

  2. #2
    Unregistered
    Guest
    Can you post your code please (or at least a section of it).

    Also, strings are terminated with a char 0x00 (or NULL). The EOF is an int that the OS returns to calls doing reads from files to let you know there's no more data to be had. EOF does not terminate a string.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    44

    Re: testing for EOF with strings

    Originally posted by Unregistered
    if the return value of EOF is an int, then how can you test for it with strings?
    This doesn't seem to make much sense to me, but I think I might have an idea what the problem is.

    EOF is a macro which is a value of type int. It is returned by many of the C file IO functions, but is not the only way in which end-of-file is detected. Functions which return char pointers (like fgets) do NOT return EOF on end-of-file - they return a NULL pointer.

    I currently have a pointer to the string and I use it to test chars. When I try setting up a loop to test for the EOF at the end of a text file and compile I get an infinite loop with puts only printing out the last word over and over. what am I doing wrong?
    Compare what you're doing with this proglet:

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

    #define LINE_LEN (2048)

    int main(void) {
    FILE * foo;
    char s[LINE_LEN];
    char * u;

    foo=fopen("myfile.txt","r");
    if (!foo) return EXIT_FAILURE;

    do {
    u=fgets(s,LINE_LEN,foo);
    if (u==NULL) break; /* end of file detected! */
    } while (u);

    fclose(foo);
    return EXIT_SUCCESS;
    }

    /* warning, untested code... but it should do! */

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or the slightly more concise
    Code:
    while ( fgets(s,LINE_LEN,foo) != NULL ) {
      // do stuff with s
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Hi everyone..Testing strings and if question
    By tinkerbell20 in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2005, 06:23 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. testing 2 strings for equality
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2001, 04:55 AM