简体   繁体   中英

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:

  • application > file : redirect stdout of app to file(write output to file)
  • application < file : redirect stdin of app from file (read input from file)
  • application >> file : redirect stdout of app to file (append output to file)
  • app1 | app2 app1 | app2 : redirect stdout of app1 to stdin of app2
  • app & : means that app should be executed in the background

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 < fileWithCommands

then my program needs to get all the commands from the fileWithCommands and execute them.

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. Use isatty(3) to figure out if this is the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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