0

This is the schema of my code (for simplicity error control removed)

main:

  freopen("error.txt","w",stderr);//redirecting stderr to error.txt
  FILE *fp=popen("./process", "w");//lunching a process
  pthread_t readerThread;

  /*reading output from process lunched in a thread otherwise it could block*/
  pthread_create(&readerThread, NULL,IsKhacToolKit::threadRead, solver);
  solver->generateDimacFile(fp);
  pclose(fp);
  pthread_exit(0);

The object solver use

fprintf(stderr,"Somes debugs Messages" --------%f seconds |%f seconds\n", (double)(start-tmp)/CLOCKS_PER_SEC, (double)start/CLOCKS_PER_SEC);

everywhere to keep trace of what happen.

My probleme is that when I lunch the executable I can see the debug messages and I dont understand why because before doing anything I redirect stderr to error.txt. What am I missing here?

What I am trying to do is lunching a process, giving him output to the threat, and then I will need to read its outPut. But apparently I don't understand how it works because here I already can't understand why debug messages are printed into the console.

Also I fclose(stderr) after comuting the output of process in the thread function.

//edit Example:

_______@voisin:~/espaces/travail/calculabilite/version2$ ./dimac_generator_is_k_HAC 10K2 5
Initializing adjList --------0.003381 seconds |0.003381 seconds
Initialiszing translater --------0.000125 seconds |0.003506 seconds
Allocating solutionParser --------0.000012 seconds |0.003518 seconds
s UNSATISFIABLE
^C
_______@voisin:~/espaces/travail/calculabilite/version2$ cat error.txt 
Preparing buffer to receive dimacFile --------0.000627 seconds |0.004145 seconds
Tranlating contraint 1 --------0.000451 seconds |0.004596 seconds
Tranlating contraint 2 --------0.000045 seconds |0.004641 seconds
Tranlating contraint 3 --------0.000010 seconds |0.004651 seconds
Tranlating contraint 4 --------0.000037 seconds |0.004688 seconds
Sending dimacFile to glucose --------0.000029 seconds |0.004717 seconds
_______@voisin:~/espaces/travail/calculabilite/version2$ 

The whole thing is not finished yet so it block. I need to Ctrl+C but you can see that before Ctrl+C debugs messages have been printed. Adding the fflush after printing allowed error.txt to not be empty anymore.

mx0
  • 6,445
  • 12
  • 49
  • 54
  • You shouldn't close stderr yourself, so don't fclose it. Perhaps try `fflush(stderr);` after printing to stderr? – P.P Nov 02 '17 at 14:15
  • i just tried, it dont work. I close stderr cause now it's just a pointer to my file erro.txt. Is not it? – Juan Pepito Nov 02 '17 at 14:27
  • `freopen` will close the old stream if necessary. I can't see what else is wrong. Try to post a [mcve]. – P.P Nov 02 '17 at 14:40
  • Adding the fflush after printing allowed error.txt to not be empty anymore. – Juan Pepito Nov 02 '17 at 15:08

0 Answers0