Making a basic shell and I need to handle file redirection. Specifically I am stumped on how I can feed data into a program like
sort
$ sort < test.txt
sort
stdin
stdin
sort
execvp()
You need to use freopen
.
It works similarly to fopen
. The first argument is the filename, the second argument is the mode, and the third argument is the file pointer to redirect. The return value is the new file pointer. So if you want to redirect stdin:
FILE *fp = freopen("input.txt", "r", stdin);
It returns a NULL pointer on failure, just like fopen
.