Thread: How to EOF??

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    167

    How to EOF??

    I have this code:

    Code:
      char c;
      int read1 = read(fd, &c, 1);
      while ( c != EOF )
      {
        write(1, &c, 1);
        read1 = read(fd, &c, 1);
      }
    Unfortunately, it's printing out an endless number of blank lines at the end, forever.

    I *think* it's because of this:

    Code:
     printf("EOF: %d, %d\n", EOF, sizeof(EOF));
    EOF = -1, and its size is 4 bytes

    So, since it's only reading 1 byte at a time, it never gets an EOF. (I *think*)

    Am I doing something wrong? Is there a way to make this work?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The function read() will return the number of bytes it read - so instead of comparing c with EOF, you need to check the return value from read().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by matsp View Post
    The function read() will return the number of bytes it read - so instead of comparing c with EOF, you need to check the return value from read().

    --
    Mats
    while ( read1 != 0 )

    Thanks again Mats!!!!

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > EOF = -1, and its size is 4 bytes
    Yes. Hence it won't fit in a char. Unless it's signed and you truncate.

    [edit]see post #6 [/edit]
    Last edited by zacs7; 05-08-2008 at 02:56 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    > EOF = -1, and its size is 4 bytes
    Yes. Hence it won't fit in a char. Unless it's unsigned and you truncate.
    Try this:
    Code:
    signed char c = EOF;
    printf("c = %d\n", c);
    It WILL print -1, even from a char. All signed types in C can hold the value -1.

    The reason "sizeof(EOF)" is 4 is that the default type is int, EOF itself is a macro that expands to -1, which then leads to sizeof(-1), and just like sizeof(0) or sizeof(42), it will give the same value as sizeof(int), becuase unless you specifically make it otherwise, numeric constants are integers.

    The other misconception here is that EOF is somehow part of the file - it is not. The end-of-file is known by the filesystem because it knows how many bytes there are in the file - and when it reaches the end of that, it refuses to read any more.

    In CPM, the end-of-file in text-files was marked with a CTRL-Z, non-text files were always a multiple of the block-size (128, 256 or 512 bytes, traditionally) [although a well-formed binary file would have information within the file to show the amount of actual content, of course]. But MS-DOS, Unix/Linux, Windows, MacOS all have exact sizes of files with no particular markers at the end [although Windows still supports CTRL-Z as a end-marker].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes I meant to say signed, don't know why I wrote unsigned.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 09:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 04:04 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM