Giving 5 Arguments but getting just 3 in terminal

喜欢而已 提交于 2019-12-02 22:46:37

问题


I want to pass a file into a c program.

If I am doing it in the IDE this arguments

./test string string < test.txt

return argc = 5, but on the terminal I am just getting argc = 3.

It seems, that its because of the "<" - symbol, I wanted to use this, to indicate that I am passing a file.

What does < mean? I am using Ubuntu with Tilix terminal


回答1:


Redirection is performed by the shell, and is not (directly) visible to your program.

./test string string < test.txt

means,

  1. Open test.txt for reading on file descriptor 1
  2. Run ./test with the arguments string and string

The program run in point 2 will inherit the parent's file descriptors, so its standard input will be connected to the opened file handle (rather than the shell's current standard input, which could be your terminal, or a different file handle).

As an aside, you probably want to avoid calling your programs test, though as long as you don't forget to invoke it with an explicit path, this is harmless.




回答2:


The < symbol will insert information from somewhere (a text file) as if you typed it yourself. It's often used with commands that are designed to get information from standard input only.

For example (using tr):

tr '[A-Z]' '[a-z]' < fileName.txt > fileNameNew.txt

The example above would insert the contents of fileName.txt into the input of tr and output the results to fileNameNew.txt.

Answer adapted from this page. For similar information about all symbols, use this page



来源:https://stackoverflow.com/questions/58977115/giving-5-arguments-but-getting-just-3-in-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!