This code takes 2 files and puts the results into a third file. It does this just fine but what's causing me concern is that the last printf is not printing to the screen. The first one prints just fine. This tells me I didn't restore the stdout correctly?
int main (int argc, char *argv[])
{
char *join[4]={"cat", "file1.txt","file2.txt", 0};
int id, fd, status;
printf("fd=%d\n", fd); //this one prints to screen
fd=dup(1); //save stdout
close(1); close stdout
FILE *out=fopen("output.c", "a"); //this is where the output should go
id=fork(); //0 for child
if (id>0) //parent process
{
waitpid(id, &status, WUNTRACED);
}
else if (id==0) //child process
{
execvp(join[0], join);
}
else //error
{
printf("error occured");
}
close(1);
dup2(fd, 1);
fclose(out);
printf("fd=%d\n", fd); //this one doesn't print to screen
return 0;
}