C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old October 21st, 2002, 06:12 PM
php_rocks php_rocks is offline
<?php
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Wyoming, USA
Posts: 41 php_rocks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
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:
int getline(char s[], int lim)
{
int c, i;

for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; i++)
s[i] = c;

if (c == '\n')
{
s[i] = c;
++i;
}
s[i] = '\0';

return i;
}


thanks,
-sean
__________________
"I speak English, can you type it?" -Everett_XML

Reply With Quote
  #2  
Old October 21st, 2002, 06:50 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
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.

Reply With Quote
  #3  
Old October 21st, 2002, 06:57 PM
php_rocks php_rocks is offline
<?php
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Wyoming, USA
Posts: 41 php_rocks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
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

Reply With Quote
  #4  
Old October 21st, 2002, 07:02 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
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.

Reply With Quote
  #5  
Old October 21st, 2002, 07:03 PM
php_rocks php_rocks is offline
<?php
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Wyoming, USA
Posts: 41 php_rocks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
so, what will be holding EOF? I/O buffer?

Reply With Quote
  #6  
Old October 21st, 2002, 07:09 PM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 510 Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level)Analyser User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 37 m 7 sec
Reputation Power: 9
Send a message via ICQ to Analyser
Quote:
you see, i want the loop to stop if EOF occurs


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/

Reply With Quote
  #7  
Old October 21st, 2002, 07:10 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
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.

Reply With Quote
  #8  
Old October 21st, 2002, 07:13 PM
php_rocks php_rocks is offline
<?php
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Wyoming, USA
Posts: 41 php_rocks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Thumbs up

hey...thanks a lot guys! i understand completely. i really appreciate your help!

-sean

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > getline (from k&r) question...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway