在C中读取命令行参数

时间:2018-09-01 14:12:20

标签: c command-line cmd arguments

我想通过这种方式将文件作为参数传递:

a.exe < testfile

如何在我的C代码中获得testfile

编辑

这是我的代码:

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

int main()
{
    char b[50];
    gets(b);
    puts(b);
    //stuff
    return 0;
}

我对此进行编译,并且当我通过这样的CL运行它时

a.exe < testfile

我明白了

`

那是为什么?我在拟议的解决方案中阅读了有关stdin的信息,但现在我被困在这里。

2 个答案:

答案 0 :(得分:1)

重定向< testfile将标准输入流替换为文件testfile

读取输入内容就像使用stdin从键盘读取数据一样。

答案 1 :(得分:1)

将其作为stdin来获取,因为这就是在shell重定向(以及可执行文件中的libc初始化)之后的结果。