gpt4 book ai didi

c - 通过终端将文本文件的内容传递给 main 时获取 (null)

转载 作者:行者123 更新时间:2023-11-30 20:18:44 26 4
gpt4 key购买 nike

我是 C 初学者,在通过终端将简单的 .txt 内容传递到 C 程序时遇到问题。

终端线:

./uloh <tests/0000_in.txt

0000_in.txt内容只是一个数字“1”

C 代码(uloh.c)

#include <stdio.h>
int main (int argc, char* argv[])
{
printf("Passed digit:\n%s\n", argv[1]);
return 0;
}

终端输出:传递的数字:(空)

但是!如果只是打电话

./uloh 1

然后输出就好了。有人可以帮我吗?

最佳答案

这个命令

./uloh <tests/0000_in.txt

不向uloh传递任何参数。 shell 会将文件 tests/0000_in.txt 重定向为 uloh 的标准输入流。

参见How do you use input redirection from a file in C?

关于c - 通过终端将文本文件的内容传递给 main 时获取 (null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53032795/

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