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
Redirection is performed by the shell, and is not (directly) visible to your program.
./test string string < test.txt
means,
test.txt
for reading on file descriptor 1./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.
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