gtpc2m34 | C/C++ Language Support User's Guide |
This function opens a file.
Format
#include <stdio.h> FILE *fopen(const char *filename, const char *mode);
This function opens the file specified by filename and
associates a stream with it. Table 4 describes the values of the mode
parameter.
Table 4. Values for the Mode Parameter
File Mode | Description |
---|---|
r | Open a text file for reading. (The file must exist.) |
w | Open a text file for writing. If the file already exists, its contents are destroyed. |
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. |
r+ | Open a text file for both reading and writing. (The file must exist.) |
w+ | Open a text file for both reading and writing. If the file already exists, its contents are destroyed. |
a+ | Open a text file in append mode for reading or for 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 | Open an empty binary file for writing. If the file already exists, its contents are destroyed. |
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+ | Open an empty binary file for both reading and writing. If the file already exists, its contents are destroyed. |
a+b or ab+ | Open a binary file in append mode for reading or for updating at the end of the file. The fopen function creates the file if it does not exist. |
Attention: Use the w, w+, wb, w+b, and wb+ parameters with care; data in existing files of the same name will be lost.
Text files contain printable characters and control characters organized into lines. Each line ends with a new-line character.
Binary files contain a series of characters. Any additional interpretation or structure imposed on a binary file is the responsibility of the application.
When you open a file with a, a+, ab, a+b, or ab+ mode, all write operations take place at the end of the file. Although you can reposition the file pointer using the fseek, fsetpos, or rewind function, the write functions move the file pointer back to the end of the file before they carry out any output operation. This action prevents you from overwriting existing data.
When you specify the update mode (using + in the second or third position), you can read from and write to the file. However, when switching between reading and writing, you must include an intervening positioning function such as fseek, fsetpos, rewind, or fflush. Output may immediately follow input if the end-of file (EOF) was detected.
Normal Return
If successful, the fopen function returns a pointer to the object controlling the associated stream.
Error Return
A NULL pointer return value indicates an error.
Programming Considerations
The TPF system does not support creating, updating, or deleting files in 1052 or UTIL state. Special files may or may not be writable in 1052 or UTIL state depending on the device driver implementation.
When a new regular file is created, if the TPF_REGFILE_RECORD_ID environment variable is set to a 2-character string, its value is used as the record ID for all pool file records that are allocated to store the contents of the file.
Examples
The following example attempts to open a file for reading.
#include <stdio.h> int main(void) { FILE *stream; /* The following call opens a text file for reading */ if ((stream = fopen("myfile.dat", "r")) == NULL) printf("Could not open data file for reading\n"); }
Related Information
See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.