
 Area: FC_prog 
  Msg#: 1742                                         Date: 04-02-96  06:14
  From: Matthew Probert                              Read: Yes    Replied: No 
    To: All                                          Mark:                     
  Subj: C File I/O - Pre Def Str

                                  C FILE I/O

                               Matthew Probert

                              Servile  Software

                             PREDEFINED STREAMS



ANSI C specifies five standard streams which are defined in stdio.h, and are 
opened by C programs automatically when they commence execution. These five 
streams are; 'stdin', 'stdout', 'stderr', 'stdprn' and 'stdaux'. 

stdin is a stream to the console (keyboard), stdout is a stream to the 
console (VDU), stderr is a second stream to the console (VDU), stdprn is a 
stream to the default printer (parallel on a PC). stdaux is rarely used. In 
a PC environment it is usually directed to COM1. 

It may be noted that printf() is actually the same as fprintf() with the 
stream stdout specified. This feature is often made use of by computer 
operators who redirect program output from outside the program. What this 
means to the programmer is that the output of printf() cannot be guaranteed 
to go to the VDU! However, what can be guarranteed is that the output will 
go to the standard output device specified by the operator. The stream 
stderr is not redirectable by the operator, and so the C programmer knows 
where the output from fprintf() calls to the stderr stream will go. They 
will go to the VDU unless the programmer tells the program to do otherwise. 

This leads us on nicely to another ANSI C file I/O function provided in the 
standard C library. freopen() allows an already open stream to be redirected 
to a specified file with specified access. It has the prototype; 

      FILE *freopen(const char *name, const char *access, FILE *stream);

It is basically the same as fopen(), except for the addition of a final 
parameter, the already open stream.

For example, to redirect all error messages to a file instead of the screen:

#include <stdio.h>

main()
{
    FILE *fp;

    fp = freopen("DEBUG.ERR","w+",stderr);

    if (fp == NULL)
        printf("\nUnable to redirect output);
}


... [Servile Info Journal]
___ Blue Wave/QWK v2.12
