| New Reply |
EOF in C |
Share Thread | Thread Tools |
| Oct3-11, 11:52 PM | #1 |
|
|
EOF in C
Can anyone please explain what is EOF in C programming language.
Is it a value (numeric, character) or something else ?? While reading the book "The C programming language", by Ritchie I came across this term. The code was : #include <stdio.h> /* copy input to output; 1st version */ main() { int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } } Please, please explain. I am just a beginner in C programming language, not an expert. So please give an explanation I can understand ... |
| PhysOrg.com |
science news on PhysOrg.com >> Hong Kong launches first electric taxis >> Morocco to harness the wind in energy hunt >> Galaxy's Ring of Fire |
| Oct4-11, 12:20 AM | #2 |
|
|
EOF is short for end of file. Think of a file handle as an object that represents something you can either read stuff from, write stuff to, or read and write stuff to. When we think of files, most of us think of files on our hard disk like the AVI movies, or MP3 files, or PDF documents. These are files, but in some platforms files relate to anything where input and output is concerned and this could include stuff from the keyboard, stuff sent and received from a network port and so. Since you are beginner just think that file means something that deals with getting input and output. What the EOF function does is return a true value if the end of the file stream has been reached (no more input left to get) and returns false if there is still stuff to read. So lets say you have a file that is 100 bytes in length and you open it at the very start. Your first getchar will get a byte and it will increment the file position to the 2nd character. When you get the 100th character the file position will then point to 101. But when you call EOF after getting the 100th character the EOF function will return true and you will get out of the loop, and you will stop reading from the file stream (the getchar command). Hope that helps! |
| Oct4-11, 12:40 AM | #3 |
|
|
What is file position ?? please explain
|
| Oct4-11, 12:41 AM | #4 |
|
|
EOF in C
please elaborate using some nice example ..
|
| Oct4-11, 01:05 AM | #5 |
|
Mentor
|
A file can be thought of as a sequence of bytes. A file pointer indicates the position in the file at which the next byte tis o be read (for an input file) or written (for an output file). There have to be millions of examples on the Web -- look 'em up. |
| Oct4-11, 01:46 AM | #6 |
|
|
But as a note to the OP, there are often function calls and not evaluations against constants that check for the EOF condition. If you open a file and get an actual handle you will have to supply the file handle to an EOF function to get whether we have an EOF condition and in this case, it is a function and not a simple comparison with a known constant. |
| Oct4-11, 03:16 AM | #7 |
|
Recognitions:
|
|
| Oct4-11, 07:25 AM | #8 |
|
|
|
| Oct4-11, 08:01 AM | #9 |
|
Recognitions:
|
|
| Oct4-11, 11:37 AM | #10 |
|
|
|
| Oct4-11, 11:40 AM | #11 |
|
Mentor
|
http://en.wikipedia.org/wiki/CP/M |
| Oct4-11, 12:11 PM | #12 |
|
Recognitions:
|
![]() Code:
> copy con myfile.txt .... type in some stuff here ... .... and finish with <ctrl-z> > |
| Oct4-11, 12:39 PM | #13 |
|
|
What is this. I can't find any relation between this code u have put above and C programming language. What shall I do after I have a file "myfile.txt" ??
|
| Oct4-11, 12:45 PM | #14 |
|
Mentor
|
|
| Oct4-11, 12:47 PM | #15 |
|
Mentor
|
|
| Oct4-11, 12:58 PM | #16 |
|
|
|
| New Reply |
| Thread Tools | |