[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


fopen

Open a file as a stream.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


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

Parameters for this facility are:

filename  
const char *  
The filename of the file to open  
mode  
const char *  
The file opening mode  

Remarks:

The fopen() function opens a file specified by filename, and associates a stream with it. The fopen() function returns a pointer to a FILE. This pointer is used to refer to the file when performing I/O operations.

The mode argument specifies how the file is to be used. "Open modes for fopen()," describes the values for mode.


UPDATE MODE

A file opened with an update mode ("+") is buffered. The file cannot be written to and then read from unless the write operation and read operation are separated by an operation that flushes the stream's buffer. This can be done with the fflush() function or one of the file positioning operations (fseek(), fsetpos(), or rewind()). Similarly, a file cannot be read from and then written to without repositioning the file using one of the file positioning functions unless the last read or write reached the end-of-file.

All file modes, except the append modes ("a", "a+", "ab", "ab+") set the file position indicator to the beginning of the file. The append modes set the file position indicator to the end-of-file.


NOTE

Write modes, even if in Write and Read (w+, wb+) delete any current data in a file when the file is opened.
Open modes for fopen():

Mode
Description
"r"  
Open an existing text file for reading only.  
"w"  
Create a new text file for writing, or open and truncate an existing file  
"a"  

Open an existing text file, or create a new one if it does not exist, for appending. Writing occurs at the end-of-file position.

"r+"  
Update mode. Open an existing text file for reading and writing (See Remarks)  
"w+"  
Update mode. Create a new text file for writing, or open and truncate an existing file, for writing and reading (See Remarks)  
"a+"  
Update mode. Open an existing text file or create a new one for reading and writing. Writing occurs at the end-of-file position (See Remarks)  
"rb"  
Open an existing binary file for reading only.  
"wb"  
Create a new binary file or open and truncate an existing file, for writing  
"ab"  
Open an existing binary file, or create a new one if it does not exist, and append. Writing occurs are the end-of-file.  
"r+b" or "rb+"  
Update mode. Open an existing binary file for reading and writing (See Remarks)  
"w+b" or "wb+"  
Update mode. Create a new binary file or open and truncate an existing file, for writing and reading (See Remarks)  
"a+b" or "ab+"  
Update mode. Open an existing binary file or create a new one for reading and writing. Writing occurs at the end-of-file position (See Remarks)  

Return:

fopen() returns a pointer to a FILE if it successfully opens the specified file for the specified operation. fopen() returns a null pointer (NULL) when it is not successful.

See Also:

"fclose"

Example of fopen() usage:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	FILE *f;
	int count;
	
	// create a new file for output
	if (( f = fopen("foofoo", "w")) == NULL) { 
		printf("Can't create file.\n");
		exit(1);
	}

	// output numbers 0 to 9
	for (count = 0; count < 10; count++)
		fprintf(f, "%5d", count);

	// close the file
	fclose(f);

	// open the file to append
	if (( f = fopen("foofoo", "a")) == NULL) { 
		printf("Can't append to file.\n");
		exit(1);
	}

	// output numbers 10 to 19
	for (; count <20; count++)
		fprintf(f, "%5d\n", count);

	// close file
	fclose(f);
	
	return 0;
}


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 16, 2000