I need to make a server that: 1) compiles a c++ file and saves the errors in a file if they exist; 2) if there are no errors i must run the a.out file and extract the output in another file. The problem resides in the first one.
In order to compile and extract errors i used more methods: 1) system("g++ file.cpp &> err.txt") - not working: it prints the errors in the console but the file remains empty 2) popen - Reference link: C: Run a System Command and Get Output? : the only difference is that i opened another file and instead of printing in the console i used fprintf to write in file. I forgot to add that the first method works if written as command in console but inside the server is problematic.
// This code is to show what i have already tried and if you find any
// syntax errors like ; or ' pls ignore them as i couldn't copy the code
// from the docker console. Thank you very much!
//1
system("g++ file.cpp &> err.txt");
if( access( "a.out", F_OK ) != -1 ) {
system("./a.out > output.txt");
//2
FILE *f;
char buff[200];
f = popen("g++ file.cpp", "r");
if (f == NULL) {
printf("Failed to run command\n" );
exit(1);
}
FILE *o;
o = fopen("err.txt", "w");
while (fgets(buff, sizeof(buff)-1, f) != NULL) {
fprintf(o, "%s", buff);
}
fclose(o);
fclose(f);
I expected the errors to be written in the err.txt not to be printed in console and in all the above examples it prints in the console and err.txt remains empty.