Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[C] Redirecting stdout <--> file

77 views
Skip to first unread message

Alex Vinokur

unread,
Mar 26, 2003, 6:30:48 AM3/26/03
to
######### C code : BEGIN #########

/*
// ============================================
// Redirecting stdout <--> file
// Sample of usage
// --------------------------------------------
// Alex Vinokur
// mailto:ale...@connect.to
// http://www.simtel.net/pub/oth/19088.html
// --------------------------------------------
// 2003-03-26
// ============================================

// =============
// Windows 2000
// DJGPP 2.03
// GNU gcc 3.2.1
// =============

// ==============
// File std_out.c
// ==============
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>


#define PRINT_ERROR(msg) \
printf ("FATAL ERROR : %s ---> %s; See %s, #%d\n", msg, strerror (errno), __FILE__, __LINE__); \
exit (1)

#define PRINT2_ERROR(msg1, msg2) \
printf ("FATAL ERROR : %s %s ---> %s; See %s, #%d\n", msg1, msg2, strerror (errno), __FILE__, __LINE__); \
exit (1)

#define PRINTF_TEST(msg) \
printf ("printf : Test#%d %s <%s, #%d>\n", \
++stepNo, msg, __FILE__, __LINE__)


int main ()
{
FILE *fp;
const int fd_stdout = 1;
int fd_outfile;
int fd_new_outfile;
int fd_save_stdout;
char* outfile_name = "file1.out";
int stepNo = 0;

printf ("\n");
printf ("\t============================\n");
printf ("\tRedirecting stdout <--> file\n");
printf ("\tSample of usage\n");
printf ("\t============================\n");
printf ("\n");


if ((fp = fopen (outfile_name, "w")) == NULL)
{
PRINT2_ERROR ("Cannot open file", outfile_name);
}

file://======== PART#1 ========
// printf to screen
file://======================

file://----------------------
fd_outfile = fileno (fp);
PRINTF_TEST ("");


file://======== PART#2 ========
// redirect stdout to file
// printf to file
file://======================

file://----------------------
if ((fd_save_stdout = dup (fd_stdout)) == -1)
{
PRINT_ERROR ("Cannot execute dup");
}
// Now fd_save_stdout and 1 are descriptors of the screen
PRINTF_TEST ("");

file://----------------------
// Close screen
if (close (fd_stdout) == -1)
{
PRINT_ERROR ("Cannot close descriptor");
}
// Now descriptor 1 (fd_stdout) is vacant
PRINTF_TEST ("");

file://----------------------
if ((fd_new_outfile = dup (fd_outfile)) == -1)
{
PRINT_ERROR ("Cannot execute dup");
}
// Now fd_outfile and 1 are descriptors of the outfile
PRINTF_TEST ("");

file://----------------------
if (close (fd_outfile) == -1)
{
PRINT_ERROR ("Cannot close descriptor");
}
// Now only descriptor 1 (fd_stdout) is descriptor of the outfile
PRINTF_TEST ("");


file://======== PART#3 ========
// redirect (restore) stdout to screen
file://======================

file://----------------------
// Close screen
if (close (fd_stdout) == -1)
{
PRINT_ERROR ("Cannot close descriptor");
}
// Now descriptor 1 (fd_stdout) is vacant
PRINTF_TEST ("");


file://----------------------
if (dup (fd_save_stdout) == -1)
{
PRINT_ERROR ("Cannot execute dup");
}
// Now fd_save_stdout and 1 are descriptors of the screen
PRINTF_TEST ("");


file://----------------------
if (close (fd_save_stdout) == -1)
{
PRINT_ERROR ("Cannot close descriptor");
}
// Now only descriptor 1 (fd_stdout) is descriptor of the screen
PRINTF_TEST ("");


file://===================
printf ("\nGood bye\n\n");
return 0;
}

######### C code : END ###########

######### Compiling & Running : BEGIN #########

% gcc -W -Wall std_out.c

% a.exe

============================
Redirecting stdout <--> file
Sample of usage
============================

printf : Test#1 <std_out.c, #73>
printf : Test#2 <std_out.c, #87>
printf : Test#7 <std_out.c, #135>
printf : Test#8 <std_out.c, #144>

Good bye

######### Compiling & Running : END ###########

######### Datafile -> file1.out : BEGIN #########
printf : Test#4 <std_out.c, #104>
printf : Test#5 <std_out.c, #112>
######### Datafile -> file1.out : END ###########


--
=================================
Alex Vinokur
mailto:ale...@connect.to
http://www.simtel.net/pub/oth/19088.html
=================================


0 new messages