Giving 5 Arguments but getting just 3 in terminal

前端 未结 2 883
情书的邮戳
情书的邮戳 2021-01-29 08:50

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 arg

相关标签:
2条回答
  • 2021-01-29 09:19

    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.

    0 讨论(0)
  • 2021-01-29 09:21

    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

    0 讨论(0)
提交回复
热议问题