checking for end of file

This is a discussion on checking for end of file within the C Programming forums, part of the General Programming Boards category; Whether I use feof() or a function with EOF, I can't get my program to really check for the actual ...

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    checking for end of file

    Whether I use feof() or a function with EOF, I can't get my program to really check for the actual end of a .exe file because apparently there are a bunch of EOF characters throughout the file. Is there a better way to find the real end of file? I finally got my copy program to work doing this:
    Code:
    while ( (bytes = fread(buf, sizeof (char), BUFSIZ, readfile) ) != NULL)
    {
      fwrite(buf, sizeof (char), bytes, writefile);
    }
    and all my variables are defined correctly...

    but it really doesn't check for end of file, it just prevents the program from writing extra characters to the end of the destination file... but without making my own function to find the real end of file, isn't there some built in function that will do the job?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    How how are opening the file? Are you sure you are opening it as a binary file?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > but it really doesn't check for end of file,
    Of course it does - that's what the comparison (which should be != 0 by the way) does.

    If it's not working, you need to post more code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    I was using fopen() and that code was the one that finally worked for me. yea i changed that NULL to a 0, got rid of the compiler warning... but when I try to do a byte at a time, like byte = getc(readfile), then check for EOF, like
    Code:
    if (byte == EOF)
    {
      break;       /*break out of for loop */
    }
    it finds an EOF character almost immediately. in one of my .exe files it found it after byte 12. then it stopped copying. like i said, the code i previously posted is what finally did work for me, but why doesn't this method work as well? or even instead of doing "if (byte == EOF)" using "if (feof(readfile) )"? neither way works the way i'm trying to use it because there is a character that the program interprets as EOF and is scattered throughout .exe programs. And by the way yes I am opening the file in binary "fopen(readfile, "rb");"
    Last edited by linucksrox; 06-01-2004 at 12:00 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'd be willing to bet byte is defined as a char. (Or perhaps unsigned, the end result is the same.) Right?

    If you're testing a byte for EOF, you can't use a char. Use an int instead.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    Yes, I am defining byte as a char, but why does that make a difference? isn't a char saved the same way as an int? and are you telling me if i just change it to an int it should work how I want? that'd be awesome...
    EDIT: nevermind I'm dumb since I didn't read the FAQ sooner. Or at least find out what EOF is. I'm just reading about EOF in the FAQ now and that clears everything up. it's defined as an int and has a negative value... that's why it makes a difference. and i changed my code and it works great
    Last edited by linucksrox; 06-01-2004 at 12:36 AM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    You did use an int, and not a char for byte right?
    Code:
    int ch;
    while ( (ch=getc(readfile)) != EOF ) {
    }
    > because there is a character that the program interprets as EOF and is scattered throughout .exe programs.
    Which is why you store it in an int
    EOF is a value distinct from every other possible value of a character (all 256 of them). If you store it in a character, then its bound to equal one of those characters at some point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    yea now byte is an int and it works a lot better, and i even understand why haha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21