w3resource

C freopen() function

C library function - freopen()

The freopen() function is used to close the file that is currently associated with stream and reassigns stream to the file that is specified by filename. With the freopen() function, a new file associated with stream is opened with the given mode, which is a character string specifying the type of access requested.

Syntax:

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

freopen() parameters:

Name Description Required /Optional
filename The name of the file to be opened. Required
mode The mode argument points to a string. If the string is one of the following, the file shall be opened in the indicated mode. Otherwise, the behavior is undefined. Required
stream Identifies an address for a file descriptor, which is an area of memory associated with an input or output stream. Required

The possible values for the mode parameter:

Mode Description
r Open a text file for reading. The file must exist.
w Create a text file for writing. If the given file exists, its contents are destroyed unless it is a logical file.
a Open a text file in append mode for writing at the end of the file. The fopen() function creates the file if it does not exist and is not a logical file.
r+ Open a text file for both reading and writing. The file must exist.
w+ Create a text file for both reading and writing. If the given file exists, its contents are cleared unless it is a logical file.
a+ Open a text file in append mode for reading or updating at the end of the file. The fopen() function creates the file if it does not exist.
rb Open a binary file for reading. The file must exist.
wb Create an empty binary file for writing. If the file exists, its contents are cleared unless it is a logical file.
ab Open a binary file in append mode for writing at the end of the file. The fopen function creates the file if it does not exist.
r+b or rb+ Open a binary file for both reading and writing. The file must exist.
w+b or wb+ Create an empty binary file for both reading and writing. If the file exists, its contents will be cleared unless it is a logical file.
a+b or ab+ Open a binary file in append mode for writing at the end of the file. The fopen() function creates the file if it does not exist.

Return value from freopen()

  • Upon successful completion, freopen() shall return the value of stream. Otherwise, a null pointer shall be returned, and errno shall be set to indicate the error.

Example: freopen() function

This following example closes the stream1 data stream and reassigns its stream pointer. stream1 and stream2 will have the same value, but they will not necessarily have the same value as stream.

#include <stdio.h>
#define  MAX_LEN  100
 
int main(void)
{
   FILE *stream, *stream1, *stream2;
   char line[MAX_LEN], *result;
   int  i;
 
   stream = fopen("test.txt","r");
   if ((result = fgets(line,MAX_LEN,stream)) != NULL)
       printf("The string is: %s\n", result);
 
   /* Change all spaces in the line to '*'. */
   for (i=0; i<=sizeof(line); i++)
     if (line[i] == ' ')
         line[i] = '*';
 
   stream1 = stream;
   stream2 = freopen("", "w+", stream1);
   fputs( line, stream2 );
   fclose( stream2);
}

Output:

The string is: Important information

 

C Programming Code Editor:

Previous C Programming: C fread()
Next C Programming: C fseek()



Follow us on Facebook and Twitter for latest update.