Reputation: 243
Full disclosure, this is an assignment for my intro to computer security class.
We are creating a guessing game (see questioner.c below) that asks the user for their name, and then a guess of the magic number. Upon telling the user whether their guess was too high, too low, or correct, the program exits, simple as that. The second part to the assignment is creating a "guesser" program (see guesser.c below) that plays our guessing game, and this is where my problems lie. The output for questioner is fed into the input for guesser, and the output for guesser is fed into the input for questioner.
I've tried "./questioner | ./guesser" on the terminal but the programs don't seem to be aware of each other and aren't using the stdin or stdout together like I was hoping. I feel like I'm missing something frustratingly basic but I'm at a loss and would appreciate any help than can be given.
The questioner.c file:
int main(){
int magic = 2936;
char name[30];
char temp[30];
int answer;
fputs("What is your name?\n", stdout);
fgets(name, 30, stdin);
fputs("What is the magic number, test?\n", stdout);
fgets(temp, 10, stdin);
answer = strtol(temp, NULL, 0);
if(answer < magic){
fputs("TOO LOW\n", stdout);
return -1;
}
if(answer > magic){
fputs("TOO HIGH\n", stdout);
return -1;
}
if(answer == magic){
fputs("SUCCESS\n", stdout);
return 0;
}
}
The guesser.c file:
int main(){
char input[50];
fgets(input, 50, stdin);
if(strcmp(input, "What is your name?") == 0){
fputs("AndyG\0\n", stdout);
}
else
fputs("???\0\n", stdout);
fgets(input, 50, stdin);
if(strcmp(input, "What is the magic number, AndyG?")){
fputs("2936\0\n", stdout);
}
else
fputs("???\0\n", stdout);
return 0;
}
Upvotes: 3
Views: 792
Reputation: 58848
Bidirectional piping in Linux is a bit tricky. The easiest way may be to use a FIFO, which is a pipe that has a filename. You can still use the |
pipe for one direction:
mkfifo my_fifo
./questioner < my_fifo | ./guesser > my_fifo
Upvotes: 1