LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 07-30-2005, 03:27 AM   #1
DaHammer
Member
 
Registered: Oct 2003
Location: Planet Earth
Distribution: Slackware, LFS
Posts: 561

Rep: Reputation: 30
Using > to redirect to file


Is there anyway to force output from a program to a file immediately as the output occurs? It seems that when using > to redirect the output that the file only gets written after program terminates. Here's a simple test to demonstrate what I mean:
Code:
#include <stdio.h>
#include <unistd.h>

int main ( void )
{
  int i = 0;
  while (i<10)
  {
    sleep (1);
    printf ("%d\n",i++);
  }
  return 0;
}
./test > test.log

If I allow the execution to terminate naturally, then test.log contains the expected output of the program. But if I killl the process with a CTRL+C, then nothing ever gets written to test.log. I persume the terminal buffers the output and only writes it to file after the program terminates, and since I kill the process it never gets a chance to write it to test.log.

Anyway, I realize that I could simply open a file handle and write to the log file from within the program, but I really don't want to do that since there are an enormous amount of "printf" calls that would need to be replaced and having to deal with the signals. Not to mention that the program must also run under windows.
 
Old 07-30-2005, 04:41 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Use fflush() after each printf(), like this:
Code:
#include <stdio.h>
#include <unistd.h>

int main ( void )
{
  int i = 0;
  while (i<5)
  {
    sleep (1);
    printf (":%d\n",i++);
    fflush(stdout);
  }
  return 0;
}

Last edited by Hko; 07-30-2005 at 04:43 AM.
 
Old 07-30-2005, 11:22 AM   #3
DaHammer
Member
 
Registered: Oct 2003
Location: Planet Earth
Distribution: Slackware, LFS
Posts: 561

Original Poster
Rep: Reputation: 30
Well, that still requires me to modify the program, but it's easier than adding a log feature. Thanks.
 
Old 08-05-2005, 09:10 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by DaHammer
Well, that still requires me to modify the program
That's true.
Here's a solution that requires only one extra line of code to set stdio-buffering to line-buffering. Line-buffering means all output will be fflush()-ed after each newline ('\n').

If you need unbuffered output (i.e. you need to have data written before a newline is output), see "man setbuf" for your options.

Code:
#include <stdio.h>
#include <unistd.h>

int main(void)
{
    int i = 0;

    setlinebuf(stdout);  /* needed only once! */

    while (i < 5) {
        sleep(1);
        printf(":%d\n", i++);
    }
    return 0;
}
 
Old 08-07-2005, 12:05 AM   #5
DaHammer
Member
 
Registered: Oct 2003
Location: Planet Earth
Distribution: Slackware, LFS
Posts: 561

Original Poster
Rep: Reputation: 30
Neat, trick, thanks!! Learn something new everyday, . Anyway, the main reason I was looking for a solution that didn't involve modifing the source was because I didn't want to have setup MSVC to rebuild the windows executable. But I went ahead and did. Anyway, thanks again guys for the help.
 
  


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
trying to redirect text to a file to cat at later point. says file doesn't exist. dr_zayus69 Programming 1 10-02-2005 08:10 AM
how do i redirect stderr to a file? nodger Programming 5 11-28-2004 11:22 AM
How do I redirect something to a file without it overwriting the destination file??? nikold01 Linux - General 8 09-09-2004 02:30 PM
Redirect to file (after startup) podollb Linux - Software 0 05-27-2004 05:29 PM
redirect into C++ without a file ? C++Boar Programming 1 04-24-2004 10:20 AM


All times are GMT -5. The time now is 04:40 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