Thread: problem compiling code and EOF

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    46

    problem compiling code and EOF

    I was doing a little programming problem given to me by an instructor and am having problems with a while loop and an EOF.

    the code

    FILE* input;
    while(1)
    {
    /*a ton of stuff, some reads from the file, but nothing else and no errors*/

    if (input == EOF)
    break;
    }


    does anyone know why this won't compile, and why i can't use while(input != EOF), which also doesn't compile??


    the errors VS6 gives me:

    prog.cpp(102) : error C2446: '==' : no conversion from 'const int' to 'struct _iobuf *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

    prog.cpp(102) : error C2040: '==' : 'struct _iobuf *' differs in levels of indirection from 'const int'


    thanks!

    -Max
    [email protected]

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >if (input == EOF)
    EOF is defined as an integer, input is defined as a FILE * so there's a type mismatch. input will never have the value EOF, but the data that you read from input will. You can either test the variable that you use to read from input for EOF or use this instead:
    Code:
    if ( feof ( input ) )
      break;
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Hi!
    Donīt use EOF. It wonīt work together with binary files, because in a binary file could be a byte with the defined value of EOF (which can be different on different systems), but it isnīt the end of the file yet. So it wonīt work like expected. EOF works only together with text-files always properly.

    klausi
    When I close my eyes nobody can see me...

  4. #4
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    When reading in a file, it is often useful to know its size without relying on the EOF (for reasons Klausi mentioned). Here is a simple way to find filesize on a POSIX system:
    Code:
    #include <sys/types.h>
    #include <sys/stat.h>
    
    struct stat buf;
    const char *path = "/home/jdeckard/testfile";
    off_t filesize;
    
    stat( path, &buf );
    filesize = buf.st_size;
    I'm not checking for errors for the sake of clarity, but make sure you do so in your application :) stat() will return -1 and set errno if something goes wrong.

    Here is a less efficient, yet ANSI compliant, solution:
    Code:
    #include <stdio.h>
    
    FILE *fptr;
    int filesize;
    
    fptr = fopen( "/home/jdeckard/testfile", "r" );
    
    fseek( fptr, 0, SEEK_END );
    filesize = ftell( fptr );
    
    fclose( fptr );
    If you decide not to close your file right away, keep in mind that the file descriptor refers to the end of the file. You will want to move it before doing any reads.
    Jason Deckard

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Hi!
    Thanks Deckard! I often needed to find a fileīs size and I did it more inefficient.

    klausi
    When I close my eyes nobody can see me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem in K&R tutrial
    By elwad in forum C Programming
    Replies: 5
    Last Post: 04-12-2009, 04:34 PM
  2. Problem with progress code
    By JFonseka in forum C Programming
    Replies: 13
    Last Post: 04-25-2008, 05:03 PM
  3. Simplifying code
    By Taka in forum C Programming
    Replies: 1
    Last Post: 10-22-2006, 02:12 AM
  4. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM