Recognising End of File

This is a discussion on Recognising End of File within the C Programming forums, part of the General Programming Boards category; Hi, I'm writing a program which reads changes STDIN to a text file, forks and execs the commads in there. ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    13

    Recognising End of File

    Hi,
    I'm writing a program which reads changes STDIN to a text file, forks and execs the commads in there. It works and execs the commands but I'm struggling to find how to recognise the end of the file. I have tried running this in a while (!EOF) loop but i'm not really sure if this is right and if so around which bits it should exist. Should it just be around the exec segment or should it be around the entire program?

    I'd really appreciate some guidance

    Many Thanks

    forg

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I guess this is dependent on what functions you are using.

    Are you actually having a problem, or just looking for an opinion? I doubt you actually want the entire program inside a while (!EOF) loop...but maybe...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    13
    I guess I didn't really mention my problem. I don't know how to recognise the end of file so I always end up in a loop with the output of "unrecognised command" because it is obviously still reading the file for input but there is nothing there. I need a way to get the program to recognise when there is nothing else in the input file.

    Thank you

    Forg

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by forgery View Post
    II always end up in a loop with the output of "unrecognised command" because it is obviously still reading the file for input but there is nothing there
    Maybe you mean "I guess it is" instead of "it is obviously"?

    The paranoid (and foolproof) method is to use stat or whatever system dependent function you have to get the actual length of the file in bytes and then keep track.

    The more normative (and often successful) method would be to just use a function that recognizes EOF, which most of them do.

    UNLESS you mean the "file" is not a real file on disk but rather your redirection of stdin.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    13
    Ok, thanks

    the file is a real file so that's fine. So how do I recognise EOF then? If EOF then set a flag? Then would I change STDIN back to the keyboard? Or is it a way I'm not thinking?

    This is where i'm pretty stumped

    Many Thanks

    forg

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by forgery View Post
    the file is a real file so that's fine. So how do I recognise EOF then? If EOF then set a flag? Then would I change STDIN back to the keyboard? Or is it a way I'm not thinking?
    1) depends how you are reading from the file. EG. fread() will return 0 if there is nothing left.
    2) depends how you took STDIN away.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    13
    I'm simply opening a file descriptor which is the input file
    Assiging the file to stdin
    Running the code to fork, exec and run the commands.
    It then carries on running like carriage return is forever being entered, which I assume is because the file is empty.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by forgery View Post
    It then carries on running like carriage return is forever being entered, which I assume is because the file is empty.
    By "It" you mean:
    1) fread()
    2) some C function
    3) some function
    4) some C
    5) this thread
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    13
    I'm not using fread in anyway. STDIN is set to the file. The file contains commands on each line e.g

    ls -l
    pwd


    The program goes through this file running the commands. Once it has ran out of commands to run, the program just prints like it is recieving a carriage return which is because the rest of the file is empty.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by forgery View Post
    STDIN is set to the file.
    How? and even better -- why do that like this? If you are doing like a learning experiment, cool, otherwise I'd say that method could be unwieldy and unorthodox.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. read a FAQ about using EOF as a loop condition (you should not)
    2. feof() function returns the eof-flag after the read failed, EOF is a constant that on most compilers == -1

    3. check the return value of the function you use to read from the stream, read the man page to learn which return value indicates the end of file
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  3. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 02:30 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  5. checking for end of file
    By linucksrox in forum C Programming
    Replies: 7
    Last Post: 06-01-2004, 02:41 AM

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