LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-28-2008, 04:36 PM   #1
rsood
LQ Newbie
 
Registered: Apr 2008
Posts: 3

Rep: Reputation: 0
'\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?
 
Old 08-28-2008, 06:51 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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.

Last edited by ta0kira; 08-28-2008 at 06:55 PM.
 
Old 08-30-2008, 08:08 PM   #3
rsood
LQ Newbie
 
Registered: Apr 2008
Posts: 3

Original Poster
Rep: Reputation: 0
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...
 
Old 08-30-2008, 08:30 PM   #4
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
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.

Last edited by pinniped; 08-30-2008 at 08:46 PM.
 
Old 08-31-2008, 12:14 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get the absolute path of file using file descriptor. appas Programming 7 01-19-2012 11:47 AM
Simplest way to output nth line of file to stdout? lumix Linux - Newbie 2 05-18-2008 09:41 PM
apt-file returns nothing; 'bad file descriptor' overbored Debian 3 10-03-2004 09:13 PM
using a socket descriptor opened by another process sudheernair Programming 0 04-16-2004 06:29 AM
XSession-errors file eats my hard drive alexbgd Linux - Newbie 5 10-27-2003 03:31 PM


All times are GMT -5. The time now is 07:26 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration