fopen

Opens a file and associates a stream with it

Local Servers:blocking
Remote Servers:blocking
Classification:ANSI
Platform:NLM
Service:Stream I/O

Syntax

  #include <stdio.h>  
   
  FILE *fopen  (  
     const char   *filename,   
     const char   *mode);
  

Parameters

filename
(IN) Points to the name of the file to be opened.
mode
(IN) Points to the file mode.

Return Values

The fopen function returns a pointer to the object controlling the stream. This pointer must be passed as a parameter to subsequent functions for performing operations on the file. If the open operation fails, fopen returns NULL. If an error occurs, errno is set.

Remarks

This function also works on the DOS partition.

The fopen function opens the file whose name is the string pointed to by filename, and associates a stream with it. The argument mode points to a string beginning with one of the following sequences:

r

Opens file for reading; uses default file translation

w

Creates file for writing, or truncates to zero length; uses default file translation.

a

Appends; opens or creates text file for writing at end-of-file; uses default file translation.

rb

Opens binary file for reading.

rt

Opens text file for reading.

wb

Creates binary file for writing, or truncates to zero length.

wt

Creates text file for writing, or truncates to zero length.

ab

Appends; opens or creates binary file for writing at end-of-file.

at

Appends; opens or creates text file for writing at end-of-file.

r+

Opens file for update (reading and/or writing); uses default file translation.

w+

Creates file for update, or truncates to zero length; uses default file translation.

a+

Appends; opens or creates file for update, writing at end-of-file; uses default file translation.

r+b

Opens binary file for update (reading and/or writing).

r+t

Opens text file for update (reading and/or writing).

w+b

Creates binary file for update, or truncates to zero length.

w+t

Creates text file for update, or truncates to zero length.

a+b

Appends; opens or creates binary file for update, writing at end-of-file.

a+t

Appends; opens or creates text file for update, writing at end-of-file.

rb+

Opens binary file for update (reading and/or writing).

rt+

Opens text file for update (reading and/or writing).

wb+

Creates binary file for update, or truncates to zero length.

wt+

Creates text file for update, or truncates to zero length.

ab+

Appends; opens or creates binary file for update, writing at end-of-file.

at+

Appends; opens or creates text file for update, writing at end-of-file.

Opening a file with read mode (r as the first character in the mode argument) fails if the file does not exist or if it cannot be read. Opening a file with append mode (a as the first character in the mode argument) causes all subsequent writes to the file to be forced to the current end-of-file, regardless of previous calls to the fseek function. When a file is opened with update mode (+ as the second or third character of the mode argument), both input and output can be performed on the associated stream.

NOTE:For an example of how to reverse the effect of redirecting stdin, see the example for fdopen.

See Also

fclose, fcloseall, fdopen, freopen

fopen Example

  #include <stdio.h>  
   
  main ()  
  {  
     char   filename[13];  
     FILE   *fp;  
     strcpy (filename, "REPORTAA.DAT");  
     fp = fopen (filename, "r");  
  
  /* Do something */
  
     fclose (fp);
  }