如果 C 中没有给出文件,则使用 stdin 和 stdout

Kap*_*ski 1 c stdin stdout file-handling

我有一个程序可以将源文件复制到目标文件。

如果用户只提供 1 个文件或都不提供这些文件,我想使用 stdin 或 stdout。

例如:命令行参数中未提供源文件名,但提供了目标文件名。程序应该从标准输入读取输入并写入给定的目标文件。

我知道,freopen()但我不知道在这种情况下应该如何使用它。

下面是我认为逻辑是如何完成的样板代码,但我找不到任何对我学习有帮助的示例。任何见解都值得赞赏。

char *src = NULL; (unless user provides in preceding code not shown)
char *dest = NULL; (^^)
        // open files based on availability

        // src and dest not provided, read from stdin and write to stdout
        if (src == NULL && dest == NULL) {
            FILE *in = freopen(src, "r", stdin);
            FILE *out = freopen(dest, "w", stdout);

            // TODO

            fclose(in);
            fclose(out);

            // src not provided, read from stdin
        } else if (src == NULL) {
            FILE *in = freopen(src, "r", stdin);

            // TODO

            fclose(in);

            // dest not provided, write result to stdout
        } else {
            FILE *out = freopen(dest, "w", stdout);

            // TODO

            fclose(out);
        }
Run Code Online (Sandbox Code Playgroud)

chq*_*lie 7

我倾向于避免freopen并使用不同的方法。我定义了两个变量,如果提供了文件名,FILE *则使用它们;如果没有提供文件名,则将它们设置为 或:fopen()stdinstdout

#include <stdio.h>

/* copying files: 0, 1 or 2 arguments */
int main(int argc, char *argv[]) {
    FILE *in = stdin;
    FILE *out = stdout;
    char *srcfile = NULL;
    char *destfile = NULL;
    int c;

    if (argc > 1) {
        srcfile = argv[1];
        if (argc > 2)
            destfile = argv[2];
    }
    if (srcfile && strcmp(srcfile, "-")) {
        if ((in = fopen(srcfile, "r")) == NULL) {
            perror(srcfile);
            return 1;
        }
    }
    if (destfile && strcmp(destfile, "-")) {
        if ((out = fopen(destfile, "w")) == NULL) {
            perror(destfile);
            return 1;
        }
    }
    while ((c = getc(in)) != EOF) {
        putc(c, out);
    }
    if (in != stdin)
        fclose(in);
    if (out != stdout)
        fclose(out);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)