我正在开始一个 C 项目,我的教授希望我们使用以下格式来运行我们的程序:
./prog stop_words_dir < chapter.txt
据我了解,这意味着我正在执行名为 prog
的程序它将一个目录作为其参数(在本例中为 stop_words_dir
)并使用该目录中名为 chapter.txt
的特定文件.
- 我说的对吗?
- 如果是这样,这种格式是否特定于 Unix(更具体地说是 bash)?
- Windows 中的等效命令是什么样的?
- 使用
<
有什么好处?而不是第二个参数 - 我如何访问
chapter.txt
在具有给定格式的 C 程序中? (我知道如何使用两个参数执行此操作)
最佳答案
- Am I correct?
不完全是。 shell(任何类 Unix 的 shell——当然是 Bash,还有 Bourne、Korn 和其他 shell,甚至包括 C shell 系列)运行名为 prog
的程序。从当前目录,使用字符串 stop_words_dir
作为参数,其标准输入从当前目录中名为 chapter.txt
的文件中读取.
- If so, is this format Unix (more specifically Bash) specific?
唯一使这个 Unix 特定的是使用 /
.在 Windows 上,使用 native shell,/
会导致混淆(当然是对于 cmd.exe
'shell')。
- What does the equivalent command in Windows look like?
prog stop_words_dir < chapter.txt
- What is the benefit to using the '<' as opposed to a second parameter.
首先,shell 打开文件并在文件不存在时处理错误。如果<
没有被使用,你的程序将不得不打开,并处理错误。其次,您可以只使用 scanf()
和 getchar()
从标准输入读取,而不需要使用 fscanf()
或 getc()
(或亲属)读取数据。这只是一个很小的好处。通常,您编写程序以便它处理任何给定的文件流,并简单地传递 stdin
当程序需要从标准输入读取时。该函数更通用 — 并且可重用。
- How would I access
chapter.txt
in a C program (with the given format. I am aware of how to do this with two parameters)?
使用从标准输入读取的标准 I/O 函数,例如 scanf()
或 getchar()
, 或者使用通用 I/O 函数但指定 stdin
作为文件流(例如 fgets()
或 fread()
— 或 fscanf()
或 getc()
或……)。
关于c - 终端C程序输入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36538614/