Difference between revisions of "$feof"
From Scriptwiki
m (Reverted edit of Albie, changed back to last version by Doomie) |
(Added more information about what each section of code is doing.) |
||
Line 4: | Line 4: | ||
Return the results of the last file access attempt in any script. | Return the results of the last file access attempt in any script. | ||
+ | Simlar to [[$fopen]](<name>).eof will return [[$true]] or [[$false]] for a specific file handler. | ||
== Example == | == Example == | ||
Line 11: | Line 12: | ||
three | three | ||
− | + | If we want to search moo.txt read in the 3rd line and echo the line and check if we're at the end of the line, it can be done like this: | |
; at first we have to open the file. | ; at first we have to open the file. | ||
− | [[ | + | [[fopen]] moo moo.txt |
; let's jump to the third line. | ; let's jump to the third line. | ||
− | [[ | + | [[fseek]] -l moo 3 |
; and now read this line so that the pointer is set to the end of the line. | ; and now read this line so that the pointer is set to the end of the line. | ||
[[echo]] -a [[$fread]](moo) | [[echo]] -a [[$fread]](moo) | ||
; finally, we want to echo $feof and see, what it returns. | ; finally, we want to echo $feof and see, what it returns. | ||
− | + | echo -a $feof | |
; this will return ''1'' as we are at the end of our file. | ; this will return ''1'' as we are at the end of our file. | ||
− | ; never forget to close the file | + | ; never forget to close the file handler. |
− | [[ | + | [[fclose]] moo |
− | |||
− | |||
− | + | Below is an example of how to loop though the file one line at a time. | |
+ | ; open the file handler. | ||
fopen moo moo.txt | fopen moo moo.txt | ||
; as long as $feof returns 0, this while loop will read the next line | ; as long as $feof returns 0, this while loop will read the next line | ||
Line 36: | Line 36: | ||
; close the file. | ; close the file. | ||
fclose moo | fclose moo | ||
− | |||
[[Category:File Handling]] | [[Category:File Handling]] |
Latest revision as of 17:16, 2 March 2008
Indicates the end of file was reached.
$feof
Return the results of the last file access attempt in any script.
Simlar to $fopen(<name>).eof will return $true or $false for a specific file handler.
Example
Let's imagine we have the following example file moo.txt in our mircdir:
one two three
If we want to search moo.txt read in the 3rd line and echo the line and check if we're at the end of the line, it can be done like this:
; at first we have to open the file. fopen moo moo.txt ; let's jump to the third line. fseek -l moo 3 ; and now read this line so that the pointer is set to the end of the line. echo -a $fread(moo) ; finally, we want to echo $feof and see, what it returns. echo -a $feof ; this will return 1 as we are at the end of our file. ; never forget to close the file handler. fclose moo
Below is an example of how to loop though the file one line at a time.
; open the file handler. fopen moo moo.txt ; as long as $feof returns 0, this while loop will read the next line while (!$feof) { ; echo the line where the pointer is. The pointer will automatically moved to the next line . echo -a $fread(moo) ; that's why we do not need to increase a variable, as you normally would have to do. } ; close the file. fclose moo