C programming freopen stdio.h


FILE *freopen(const char * restrict filename,
const char * restrict mode,
FILE * restrict stream);

Parameters:
filename -The name of the file to be opened

mode -The mode of the file.The mode is same as the mode for ‘fopen’ function.

ModeFunction
ropen text file for reading
wtruncate to zero length or create text file for writing
aappend; open or create text file for writing at end-of-file
rbopen binary file for reading
wbtruncate to zero length or create binary file for writing
abappend; open or create binary file for writing at end-of-file
r+open text file for update (reading and writing)
w+truncate to zero length or create text file for update
a+append; open or create text file for update, writing at end-of-file
r+b or rb+open binary file for update (reading and writing)
w+b or wb+truncate to zero length or create binary file for update
a+b or ab+append; open or create binary file for update, writing at end-of-file

For more information of how to use the mode visit C fopen stdio.h,a detail explanation is given there.

stream -The ‘FILE*’ stream to be associated with the file to be opened.

Return type
FILE*-If file opening fail NULL is returned else the value of the stream is written.

In C programming,like the ‘fopen’ the freopen function is to open a file but rather than creating a new stream, freopen( ) associates the file with the existing stream specified by the third argument.The file previously associated with that stream is closed.You may use ‘freopen’ to open a file but it is mainly use for associating a new file to the existing stream.The most common use of freopen( ) is to redirect the standard streams,stdin,stdout and stderr.

Note** in associating the existing steam to the new file ‘freopen’ attempts to close the previously associated file
Code example

FILE *fp=fopen( “text.txt” , “w” ) ;

fputs( “My text file” , fp ); //writing text to file

freopen( “New text.txt” , “w+” , fp ); //work fine,associate existing stream ‘fp’ to new text file “new text.txt”

fputs( “My new text file” fp ); //writing string to “New text.txt” file

fclose( fp );

In calling “( “New text.txt” , “w+” , fp )” the previous file “text.txt” is closed.

Open the files “text.txt” and “New text.txt” you will see your strings “My text file” and “My new text file” written on it.





Some points to note

If the first argument is a NULL pointer
i)If the first argument passed is a NULL pointer the ‘freopen’ function attempts to change the mode of the stream to that specified by the second argument.It behave as thought the filecurrently associated with the stream had been used.It is implementation-defined which changes of mode are permitted and under what circumstances.

Code example

FILE *fp=NULL;

char *fname=NULL ;

fp=freopen( fname , “w” , fp ) ;

if( fp==NULL )
{
printf(“File open failed”);
}
else
  printf(“File opened”);

Ouput,

File open failed


If freopen fail to close the file

‘freopen’ can associate the new file to the existing stream,in doing so it will try to close the previously associated file,but if it fail to close the file it will be ignored.The error and end-of-file indicators for the stream are cleared.


Related links

->C fopen stdio.h

->C fclose stdio.h