|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
getline (from k&r) question...
the k&r says this will return 0 when EOF is encountered....well, what if EOF is encountered when i is 20 or something?
Quote:
thanks, -sean
__________________
"I speak English, can you type it?" -Everett_XML |
|
#2
|
||||
|
||||
|
If you notice, the last line in the function returns the value of i (whatever it happens to be currently). So if i is 20 when EOF is encountered, then this function returns 20.
|
|
#3
|
|||
|
|||
|
but 20 is not a valid EOF value. 0 is....
lemme show you what the loop looks like: while ((len = getline(line, MAXLINE)) > 0) you see, i want the loop to stop if EOF occurs but how would it stop if EOF occurs at the 20th character or something? btw, this is on page 29 of the k&r. -sean |
|
#4
|
||||
|
||||
|
Well, your code will get the value 20 on this pass, but when the next time getline() is called, it will return 0 right away, since EOF had been reached the previous time. So your code will break out of the loop on the next pass.
|
|
#5
|
|||
|
|||
|
so, what will be holding EOF? I/O buffer?
|
|
#6
|
||||
|
||||
|
Quote:
Making some assumptions about your motives, and being a bit more precise, I'd say you want to stop the loop when there is no more input available. If the buffer (s[] in this case) isn't full (less than lim), but not empty either when the EOF occurs, you probably still want to process the data that's still in s[], right? The thing is, once end-of-file is reached, getchar() will always return EOF. So, the buffer isn't completely full, an EOF is read and getline() returns. The next time getline() is called, getchar() returns EOF immediately, causing getline() to return 0. Maybe a rewording might help: getline() returns 0 only when EOF is reached.
__________________
"A poor programmer is he who blames his tools." http://analyser.oli.tudelft.nl/ |
|
#7
|
||||
|
||||
|
I think you're misunderstanding how getline() works. Basically, it reads one line at time (into the param s) and returns the length of the line that was read in. So, if there's nothing more to read, then it returns 0 (as nothing was read in).
The only thing that will hold EOF if anything, is the variable c in the function getline(). Since it's a variable that is local to the function getline(), nothing else can read the value of it outside the function. Hope this clears things up a bit. |
|
#8
|
|||
|
|||
|
hey...thanks a lot guys! i understand completely. i really appreciate your help!
![]() -sean |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > getline (from k&r) question... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|