简体   繁体   English

使用operator <in C从文件重定向

[英]Redirection from files using operator < in C

I wrote a program that simulate the bash command in Linux, in C. It works perfectly with inputs from the keyboard , meaning: 我在C中编写了一个模拟bash命令的程序,它完全适用于键盘的输入,意思是:

  • application > file : redirect stdout of app to file(write output to file) application > file :将app的stdout重定向到文件(将输出写入文件)
  • application < file : redirect stdin of app from file (read input from file) application < file :从文件重定向app的stdin(从文件读取输入)
  • application >> file : redirect stdout of app to file (append output to file) application >> file :将app的stdout重定向到文件(将输出附加到文件)
  • app1 | app2 app1 | app2 : redirect stdout of app1 to stdin of app2 app1 | app2 :将app1的stdout重定向到app2的stdin
  • app & : means that app should be executed in the background app & :表示app应该在后台执行

All these work, when I enter the command from the keyboard, as mentioned above. 当我从键盘输入命令时,所有这些工作都如上所述。

In order to complete my assignment, I need to add one more element, which is redirection from a file. 为了完成我的任务,我需要再添加一个元素,即从文件重定向。 Meaning, if my program is called bashSimulator , then if I do this: 意思是,如果我的程序被称为bashSimulator ,那么如果我这样做:

bashSimulator < fileWithCommands

then my program needs to get all the commands from the fileWithCommands and execute them. 然后我的程序需要从fileWithCommands获取所有命令并执行它们。

I have no idea how to do the redirection from a file. 我不知道如何从文件重定向。

You read the commands from stdin instead of from a batch file or interactively from the user. 您可以从stdin而不是批处理文件中读取命令,也可以从用户以交互方式读取命令。 Use isatty(3) to figure out if this is the case. 使用isatty(3)来确定是否是这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM