#1
  1. No Profile Picture
    <?php
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Wyoming, USA
    Posts
    41
    Rep Power
    12

    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?

    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
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    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.
  4. #3
  5. No Profile Picture
    <?php
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Wyoming, USA
    Posts
    41
    Rep Power
    12
    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
    "I speak English, can you type it?" -Everett_XML
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    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.
  8. #5
  9. No Profile Picture
    <?php
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Wyoming, USA
    Posts
    41
    Rep Power
    12
    so, what will be holding EOF? I/O buffer?
    "I speak English, can you type it?" -Everett_XML
  10. #6
  11. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    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/
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    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.
  14. #8
  15. No Profile Picture
    <?php
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Wyoming, USA
    Posts
    41
    Rep Power
    12

    Thumbs up


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

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

IMN logo majestic logo threadwatch logo seochat tools logo