gpt4 book ai didi

c - linux上执行C语言输入文件

转载 作者:太空狗 更新时间:2023-10-29 12:10:21 24 4
gpt4 key购买 nike

我不明白这之间有什么不同

一个。 ./target <input
b. ./target <$(cat input)
C。 ./target $(<input)

./target是一个 C 程序,输入是一个文件或有效负载

我想知道它们有什么不同,有没有更多的技术或方法?

最佳答案

三种表示法中的两种是 Bash 特有的;这三个都是 shell 符号。运行的程序需要以完全不同的方式处理数据。

  1. ( ./target <input input redirection ):目标程序需要读取标准输入来获取信息。

    #include <stdio.h>

    int main(void)
    {
    int c;
    while ((c = getchar()) != EOF)
    putchar(c);
    return 0;
    }
  2. ( ./target <$(cat input) process substitution ):目标程序需要打开命令行参数中指定的文件名才能获取信息。

    #include <stdio.h>

    int main(int argc, char **argv)
    {
    if (argc != 2)
    {
    fprintf(stderr, "Usage: %s file\n", argv[0]);
    return 1;
    }
    FILE *fp = fopen(argv[1], "r");
    if (fp == 0)
    {
    fprintf(stderr, "%s: failed to open file '%s' for reading\n",
    argv[0], argv[1]);
    return 1;
    }

    int c;
    while ((c = getc(fp)) != EOF)
    putchar(c);

    fclose(fp);
    return 0;
    }
  3. ( ./target $(<input) command substitution ):目标程序将文件内容拆分为单词作为程序的参数,每个参数一个单词。

    #include <stdio.h>

    int main(int argc, char **argv)
    {
    int count = 0;
    for (int i = 0; i < argc; i++)
    {
    count += printf(" %s", argv[i]);
    if (count > 70)
    putchar('\n'), count = 0;
    }
    return 0;
    }

因此,所需的处理完全不同。

关于c - linux上执行C语言输入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47429135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com