![]() |
![]() |
![]() |
Open a file stream
#include <stdio.h> FILE * fopen( const char * filename, const char * mode );
libc
The fopen() function opens a file stream for the file specified by filename. The mode string begins with one of the following sequences:
You can add the letter b to the end of any of the above sequences to indicate that the file is (or must be) a binary file (this is an ANSI requirement for portability to systems that make a distinction between text and binary files, such as DOS). Under QNX, there's no difference between "text" files and "binary" files.
![]() |
When using a stream in update mode, writing can't be followed by reading without an intervening call to fflush(), or to a file-positioning function (fseek(), fsetpos() or rewind()). Similarly, reading can't be followed by writing without an intervening call to a file-positioning function, unless the read resulted in end-of-file. |
A pointer to a file stream for success, or NULL if an error occurs (errno is set).
#include <stdio.h> #include <stdlib.h> int main( void ) { FILE *fp; fp = fopen( "report.dat", "r" ); if( fp != NULL ) { /* rest of code goes here */ fclose( fp ); return EXIT_SUCCESS; } return EXIT_FAILURE; }
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
errno, fclose(), fcloseall(), fdopen(), freopen()
![]() |
![]() |
![]() |