函数如何在这里工作

函数如何在这里工作

本文介绍了getline()函数如何在这里工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白函数 getline 在这里是如何工作的。为什么换行符被排除在for循环之外,为什么要在一个单独的块中测试换行符的存在?

  #include< stdio.h> 
#define MAXLINE 1000 / *最大输入行长度* /

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

/ *打印最长的输入行* /
main()
{
int len; / *当前行长度* /
int max; / *目前所见的最大长度* /
char line [MAXLINE]; / *当前输入行* /
char最长[MAXLINE]; / *此处保存的最长行* /
max = 0; ((len = getline(line,MAXLINE))> 0)
if(len> max){
max = len;
copy(最长,行);

if(max> 0)/ *有一行* /
printf(%s,最长);
返回0;


$ b / * getline:将一行读入s,返回长度* /
int getline(char s [],int lim)
{
int c,i; (i = 0; i< lim-1&&&(c = getchar())!= EOF& c!='\\\
'; ++ i)
s [i] = c;
if(c =='\\\
'){
s [i] = c;
++ i;
}
s [i] ='\0';
返回i;
}


/ *复制:将'from'复制到'to';假设足够大* /
void copy(char to [],char [from])
{
int i;
i = 0; ((to [i] = from [i])!$'
')
++ i;


解决方案

getline 将从 stdin 中读取一行。由于行由换行符分隔('\ n'字符),因此 getline 将读取并包含换行符。对于 getline 读取换行符是没有意义的,因为它会读取多行。



1) a \'有三种情况可导致for循环停止。遇到了n'。
如果发生这种情况,它会在添加空终止符之前将新行添加到当前字符串的末尾。这就是 if(c =='\ n')的用途。



2 )读取EOF 3)读取最大字符数量。

如果发生其中任何一种情况,将新行添加到字符串末尾将被跳过,并且只会添加空终止符。



空终止符('\\ \\ 0'字符)是C如何表示字符串的结尾。


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;
}
解决方案

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.

这篇关于getline()函数如何在这里工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 06:12