I would like to redericting stdout, stderr to file and stdin from char*. My goal is make it in C.
When i run this code:
int main(){
stdout=fopen("/home/user/file.txt","w");
printf("aaaa");
printf("\nbbbb");
system("/bin/bash");
sprintf("stdin","exit");
return 0;
}
File didn’t have for some string and bash take argument from console. Where is bug??
You don’t want to assign to
stdout. Instead, you (probably) want to usefreopen, in your case like:freopen("/home/user/file.txt","w", stdout);If/when you’re doing all the processing internally, you’re generally better off writing the code to receive a
FILE *as a parameter, and passing the correct value. That doesn’t work when you have external code that writes directly tostdoutthough.Edit: I should probably also mention one other serious problem with
freopen— no method is provided to restore it to the previous stream. It’s up t you to usefreopenagain, and know the path that will write to the console (or whatever).