| | |
End of file controlled loop
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2006
Posts: 6
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
{ FILE *scorefile; int score; int temp; scorefile = fopen("scores.txt","r"); if (scorefile == NULL) printf("Error opening input file\n"); else { temp = 0; while (feof(scorefile) <= 0) { fscanf(scorefile,"%d",&score); temp = temp +1; } } fclose(scorefile); printf("Temp = %d\n",temp); system("pause"); return(0); }
An end-of-file-controlled loop is used to read values until the end of file is encountered.
This sort of loop uses the fact that a file goes into the fail state when you try to read a
data value beyond the end of a file to control it.
The value of "temp" will be 6 at the end of the program but it was 0 at the beginning. The
value of temp is determined by its placement in the end-of-file controlled loop whereby
it loops once for every individual term it encounters and adds the value of 1 for
each. After each iteration it checks with the condition enclosed within the parentheses
that "feof(scorefile) . . ."
Is there a better, more economical way of saying this (the file has 6 values within it)?
Thanks for viewing
![]() |
Similar Threads
- Help with calculating avrg from input file. (C++)
- ERROR--Unexpected end to file (C++)
- How to find End of file (C++)
- sentinel controlled while loop (C++)
- End of file function (C)
Other Threads in the C Forum
- Previous Thread: Problem starting a homework problem for class. Please help!
- Next Thread: help with conversion calculator
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks binarysearch calculate changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fflush fgets file fork forloop framework function getlasterror givemetehcodez grade gtkwinlinux hacking histogram inches include incrementoperators input intmain() iso kernel keyboard km license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming standard string structures systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi





