'\n' eats line when stdout opened from file descriptor
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
'\n' eats line when stdout opened from file descriptor
I have the following code
Code:
#include <unistd.h>
#include <stdio.h>
int main(void){
int fildes = dup(1);
printf("On terminal\n");
freopen("file.txt", "w", stdout);
printf("To file");
fclose(stdout);
stdout = fdopen(fildes, "w");
printf("Back to terminal?");
printf("!!!\r\f");
printf("????\n");
/*
freopen("file.txt", "a", stdout);
printf("Back to file?\n");
fclose(stdout);
stdout = fdopen(fildes, "w");
printf("Back to terminal?\n");
*/
return 0;
}
this results in output on the terminal of
On terminal
Back to terminal?!!!
the ???? does not appear. This code is the result of fiddling around a little, and from that it appears that after stdout has been redirected back to the terminal from a previously saved file descriptor, the \n causes any text printed with it to be eaten (not displayed). This happens whether fdopen is passed "w" "wb" or "w+b". Anybody know what's going on?
I'm not sure what a form feed does in a terminal, but carriage return should make the next print go to the beginning of the same line. What does it do without \f?
ta0kira
edit:
Looks like the buffer catches some of the output. It generally won't flush until a newline, so some of the printfs might be held until after the file change. You're changing stdout in a non-standard way, though.
Thanks for the reply. What you say about changing stdout in a nonstandard way probably has something to do with the problem, because when I tried compiling my code on another operating system it complained about "stdout" being an invalid lvalue. Changing the code such that the stdout reassignment line is
Code:
*stdout = *fdopen(fildes, "w");
gets it to compile and solves the problem I was having. Not sure exactly why, but...
Your solution makes sense; think of it this way: you're modifying a FILE*, not a FILE**, so you can't have any permanent effect on the variable itself although you can alter what it points to.
You're replacing the pointer to the FILE buffer itself, but like the other compiler demonstrates, it doesn't have to be a writable pointer. You should really use the open system call instead of freopen, then replace standard output with dup2(fd, STDOUT_FILENO); and close(fd);. That's the *nix way, and is the commonly-accepted way of replacing standard output. When you do that, you don't need to fclose(stdout); because the operation is beneath the C library; therefore, the state of the FILE object remains exactly the same. You should fflush(stdout); right before the switch, however.
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.