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

Open a file as a stream.
Compatibility:
This function is compatible with the following targets:
Prototype:
#include <stdio.h>
FILE *fopen(const char *filename,
   const char *mode);
Parameters:
Parameters for this facility are:
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():
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