How does getline() function work here?

喜你入骨 提交于 2019-12-06 09:50:57

问题


I do not understand how the function getline is working here. Why has the newline character been excluded from the for loop and why is it testing for the presence of newline character in a separate block?

#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */

int getline(char line[], int maxline);
void copy(char to[], char from[]);

/* print the longest input line */
main()
{
    int len; /* current line length */
    int max; /* maximum length seen so far */
    char line[MAXLINE]; /* current input line */
    char longest[MAXLINE]; /* longest line saved here */
    max = 0;
    while ((len = getline(line, MAXLINE)) > 0)
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    if (max > 0) /* there was a line */
        printf("%s", longest);
    return 0;
}


/* getline: read a line into s, return length */
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;
}


/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[])
{
    int i;
    i = 0;
    while ((to[i] = from[i]) != '\0')
        ++i;
}

回答1:


getline will read a single line from stdin. Since lines are delimited by newlines ('\n' characters), getline will read up to and including the newline. It wouldn't make sense for getline to read past the newline, because it would then be reading more than one line.

There are three cases that can cause the for loop to stop.

1) a \'n' was encountered.
If this occurs it will add the newline to the end of the current string before adding the null terminator. This is what the if (c == '\n') is for.

2) EOF is read or 3) the max amount of characters to be read, are read.
If either of these occurs, the adding of the newline to the end of the string is skipped and only the null terminator is added.

The null terminater (the '\0' character) is how C indicates the end of a string.




回答2:


The reason that they are excluding the \n in the loop and then subsequently checking for the \n is because the value of c is still allocated (still in scope) after the for loop is done. one reason it seems a bit complicated is they decided to exclude brackets. Since the function is to get the next line a line would "end" when you get the newline character "\n". To write this so it is a bit more readable it would look like:

int getline(char s[],int lim)
{
    int c,
    int 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;
}



回答3:


There are three important cases to consider when reading until the end of a line. Those being:

  1. We have reached the maximum number of bytes allowed in our buffer.
  2. We have reached the EOF and hence there is nothing else to read.
  3. We have reached the end of line character and should stop reading any further.

In the for loop, the following:

  • i < ( lim - 1 ) checks case 1.
  • ( c = getchar() ) != EOF checks case 2 (ugly).
  • c != '\n' checks case 3.

... the reason it needs to short circuit as the newline is read is because that is the intension of the function. It also reads up to the newline character and includes it in the string. The last step:

s[ i ] = '\0';

... ensures that it is a NULL terminated string (the convention in C). It then returns the number of bytes read (which is not uncommon).




回答4:


Well i came up with this question too. Here is the possible answer to your question.

1)The very first misconception in this question is getline itself. Particularly in this code getline is a user defined function. Some compiler may pop up an error. So try another name for this function(Maybe getl or something you find suitable for yourself)

2)Secondly, main() acts as an intermediate for accepting the input string. It checks whether the length of the input string (computed by function getline() ) is greater than zero and max.

3)The copy function copies the value of the longest string in the character array longest.

While printing code says : printf("%s",longest); Now a question arises. Why not a for loop to print the string. Well that is also correct but it will require more steps and iterations which is inefficient.

This is what i came up with.



来源:https://stackoverflow.com/questions/18278240/how-does-getline-function-work-here

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!