需要帮助使用函数在 C 中实现 kittycat 函数

Need help implementing kittycat function in C using a function

需要帮助获取 display_stream 函数以读取 Shell 中的标准输入。当我在 Shell 中键入 './kittycat' 时,当它应该从 stdin 读取时我变得空白。对于一个或多个参数,其他一切都有效,它会读取文本文件 (./kittycat test.txt test2.txt),如果我输入 './kittycat error.txt',它会说错误文件不是成立。'我只是缺少一种使用函数 display_stream 从 stdin 读取的方法。包括 shell 输出与预期结果的屏幕截图。

    [enter image description here][1]#include <stdio.h>
    #include <stdlib.h>
    
    void display_stream(FILE *fptr);
    
    int
    main(int argc, char *argv[])
    {
        int i;
    
        // if no args given, read from stdin (just like shell/cat)
        if (argc < 2)
            display_stream(stdin);
    
        for (i = 1; i < argc; i++) {
            FILE *fptr = fopen(argv[i], "r");
    
            if (fptr == 0) {
                printf("error: file not found.");
                continue;
            }
    
            display_stream(fptr);
            fclose(fptr);
        }
    
        return 0;
    }
    
    void
    display_stream(FILE *fptr)
    {
    
        int x;
    
        /* read one character at a time from file, stopping at EOF,
            which indicates the end of the file. */
        while ((x = fgetc(fptr)) != EOF)
            putchar(x);
    }

MY output What is expected

  1. fclose 移出 display_stream,它不属于那里。将它放在对 display_stream.
  2. 的调用之后
  3. display_stream(stdin)添加到main(这次没有fclosestdin不应该关闭),在循环之前或之后。它应该可以正常工作。

它可能会逐行从 stdin 复制,但这是由于程序外部的缓冲导致禁用 AFAIK 并不那么容易。

此外,printf( "%c", x ) 可以是 putchar(x)

检查 argc 来决定程序是应该从 stdin 读取还是应该打开 argv[i] 来打开文件。


这是重构的[和注释]代码:

#include <stdio.h>
#include <stdlib.h>

void display_stream(FILE *fptr);

int
main(int argc, char *argv[])
{
    int i;

    // if no args given, read from stdin (just like shell/cat)
    if (argc < 2)
        display_stream(stdin);

    for (i = 1; i < argc; i++) {
        FILE *fptr = fopen(argv[i], "r");

        if (fptr == 0) {
            printf("error: file not found.");
        }
        else {
            display_stream(fptr);
#if 1
            fclose(fptr);
#endif
        }
    }

    return 0;
}

void
display_stream(FILE *fptr)
{

    int x;

    /* read one character at a time from file, stopping at EOF,
        which indicates the end of the file. */
    while ((x = fgetc(fptr)) != EOF) {
        printf("%c", x);
    }

    // don't close this here -- let caller do it (e.g. stdin should _not_ be
    // closed and only caller knows whether the stream is stdin or not)
#if 0
    fclose(fptr);
#endif
}

这是一个稍微更简洁的版本:

#include <stdio.h>
#include <stdlib.h>

void display_stream(FILE *fptr);

int
main(int argc, char *argv[])
{
    int i;

    // if no args given, read from stdin (just like shell/cat)
    if (argc < 2)
        display_stream(stdin);

    for (i = 1; i < argc; i++) {
        FILE *fptr = fopen(argv[i], "r");

        if (fptr == 0) {
            printf("error: file not found.");
            continue;
        }

        display_stream(fptr);
        fclose(fptr);
    }

    return 0;
}

void
display_stream(FILE *fptr)
{

    int x;

    /* read one character at a time from file, stopping at EOF,
        which indicates the end of the file. */
    while ((x = fgetc(fptr)) != EOF)
        putchar(x);
}