I'm encountering a limitation of bash input redirection that's preventing me from accomplishing a task, and I haven't been able to find a way to work around it so far. Essentially, I need to be able to redirect two separate files to a program's stdin
separately, as opposed to all at once.
Here's an example program:
#include <stdio.h>
int main(void) {
char myArr[200][100];
int i = 0;
while (fgets(myArr[i], 100, stdin)) {
i++;
}
freopen("/dev/tty", "rw", stdin);
int choice = 0;
while (choice != 1 && choice != 2) {
printf("Enter menu input: ");
scanf("%d", &choice);
}
if (choice == 1) {
for (int j = 0; j < i; j++) {
printf("%s\n", myArr[j]);
}
} else if (choice == 2) {
exit(-1);
}
}
This program takes in input from stdin
until EOF is reached, and counts the number of successful reads (akin to counting the number of lines in the input). I'm passing a whole file's worth of input using ./a.out < longInput.txt
, which causes fgets
to stop reading once the end of longInput.txt
is reached.
I use freopen()
to reset stdin
so that I can start inputting menu options again after longInput.txt
has reached EOF
. However, this doesn't work as intended when trying to use bash redirection for menu inputs in the second part of the program.
My question is: how can I redirect input from a second input file that only contains menu options, after I've redirected from the first file and reset stdin
, without hard-coding that second file name in my call to freopen()
?
Assume that I cannot modify the source of that program, and I only have access to a compiled executable. Here are some references I've already tried, to no avail:
- Multiple input text files as stdin under unix
- Redirect multiple files to stdin
- Bash: multiple redirection
- Two redirection operators in one command
Thanks!