繁体   English   中英

从交流程序获取输出

[英]getting output from a c program

我编译了以下程序:

#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;
}

我试图在bash shell中运行它:

gcc -o longest-line longest-line.c
./longest-line

基本上,它变成了一个正在运行的进程(显示为ps aux的结果),并且光标只是闪烁。 在代码中,当程序运行并调用getline函数时,它将执行1000次迭代,并且每次从终端获取输入以调用getchar时都可以调用该方法,以便在不是文件末尾或换行符末尾的情况下增加计数器。 但是,立即在终端中没有输入,并且当我开始添加输入并按Enter键时:

$ ./longest-line
Hello World
Hello Again

什么都没发生。 它应该打印最长的行。

问题是,如果您从键盘getline按下'\\ n',由于该语句,它将始终返回1

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

并且while ((len = getline(line, MAXLINE)) > 0)行将始终为true。

但是,如果您使用文件作为标准输入,则由于EOF,它可以正常工作。

如果希望它通过键盘输入起作用,请按Ctrl-D或Ctrl-Z模拟EOF。

因此,在我的编译器上,我不得不修复一些小问题。

  • 通常,main应该写为int main() { ... }int main(int argc, char **argv) { ... }
  • getline()与从#include <stdio.h>中获取的内置函数发生冲突,因此我只是将您的getline_custom重命名为getline_custom ,还重命名了所有使用点。

就是说,通过这些次要的修复程序(在编译器中可能不需要),程序可以正常工作

我相信您的困惑是,在您发送EOF之后,程序不会打印最长的行。 在bash中,您可以使用CTRL + D进行此操作。

例:

[12:39pm][wlynch@watermelon /tmp] ./foo
test          // Then I hit enter
longest line  // Then I hit enter
short line    // Then I hit enter
one more      // Then I hit ctrl-D
longest line  // This is output from the program.

另一个例子:

如果使用重定向,我们可以更轻松地看到输入和输出之间的差异。

[12:42pm][wlynch@watermelon /tmp] printf "longest line\nshort line" | ./foo
longest line

或者,使用输入文件:

[12:53pm][wlynch@watermelon /tmp] cat input.txt 
longest line
short line
foo
blah
[12:53pm][wlynch@watermelon /tmp] cat input.txt | ./foo
longest line

另一方面

另一方面,如果您希望程序在行输入之后打印当前最长的行,那么我们将需要在该程序中更改代码。

这是一个例子:

int main() {
    int len = 0;           /* current line length */
    int max = 0;           /* maximum length seen so far */
    char line[MAXLINE];    /* current input line */
    char longest[MAXLINE]; /* longest line saved here */

    while ((len = getline(line, MAXLINE)) > 0) {
        if (len > max) {
            max = len;
            copy(longest, line);
        }

        printf("Current longest line: %s", longest);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM