Question

I have two games I created in c:

The first is a guess the number game: the computer chooses a number and the user has to guess it.

The second does the opposite: the user chooses a number and the computer has to guess it through feedback from the user (h or l)

Now I want to make the two programs play with each other. for example: I open the two programs and the first takes an input from the second and then provides the value to it.

I have found that it is possible to do this indirectly using a text file:

 #include<stdio.h>
 #include<unistd.h>
 #include<stdlib.h>


 int main()
 { 
   char line[260]
   FILE *fp;
   fp = fopen("file.txt", "r+");
   if(fr == NULL){ printf("File not found");}
   fscanf("%s", line);

     fclose(fr);
    return 0;
   }

And I would use sleep to make sure the program reads the correct data. But I don't like this way: I will have to view the results after the game is over. Is there a way I can make this work: like seeing the results while the game is on? Is it possible to read and write to an .exe file?

Was it helpful?

Solution

Probably the easiest way is to connect both programs by two pipes. There is plenty of examples creating two pipes and forking a subprocess. You may want to have a look on the function popen and also popen2 for bidirectional communication which was presented for example here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top