Thread: fgets and EOF

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    28

    fgets and EOF

    Alright, so if I use fgets how do I tell it to keep reading lines in until it reaches the end of file ?? I keep getting runtime errors if I put != EOF ... is != NULL equivalent??

    Also, if I use fgets how do I search through as I am reading it in for a specific character?? I've tried but it doesn't seem to be working.

    Thanks!

  2. #2
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Re: fgets and EOF

    Originally posted by PunkyBunny300
    Alright, so if I use fgets how do I tell it to keep reading lines in until it reaches the end of file ?? I keep getting runtime errors if I put != EOF ... is != NULL equivalent??

    Also, if I use fgets how do I search through as I am reading it in for a specific character?? I've tried but it doesn't seem to be working.

    Thanks!
    Testing something != NULL and and testing something != EOF cannot be the same. An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant, so, NULL can either be defined as 0 or (void *) 0. EOF is neccesarily a negative integer
    The one who says it cannot be done should never interrupt the one who is doing it.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Re: fgets and EOF

    Originally posted by PunkyBunny300
    Alright, so if I use fgets how do I tell it to keep reading lines in until it reaches the end of file ?? I keep getting runtime errors if I put != EOF ... is != NULL equivalent??


    fgets() will return NULL at end of file.

    Code:
     while(fgets(buffer, length, file))
    {
      //process
    }
    Also, if I use fgets how do I search through as I am reading it in for a specific character?? I've tried but it doesn't seem to be working.

    Thanks!
    Post what you have tried.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets
    By mesmer in forum C Programming
    Replies: 1
    Last Post: 11-25-2008, 12:37 AM
  2. Check for EOF when using fgets
    By daghenningsorbo in forum C Programming
    Replies: 6
    Last Post: 05-16-2007, 06:48 PM
  3. Confused about fgets, clearing the buffer, etc
    By caduardo21 in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 11:03 AM
  4. fgets
    By G'n'R in forum C Programming
    Replies: 8
    Last Post: 09-18-2003, 12:24 AM
  5. EOF Char
    By vickys in forum Windows Programming
    Replies: 2
    Last Post: 12-12-2001, 05:18 AM