c:io:fopen

C++ Reference

fopen

Syntax:

    #include <cstdio>
    FILE *fopen( const char *fname, const char *mode );

The fopen() function opens a file indicated by fname and returns a stream associated with that file. mode is used to determine how the file will be treated (i.e. for input, output, etc).

If there is an error, fopen() returns NULL.

ModeMeaning
“r”Open a text file for reading
“w”Create a text file for writing
“a”Append to a text file
“rb”Open a binary file for reading
“wb”Create a binary file for writing
“ab”Append to a binary file
“r+“Open a text file for read/write
“w+“Create a text file for read/write
“a+“Open a text file for read/write
“rb+“Open a binary file for read/write
“wb+“Create a binary file for read/write
“ab+“Open a binary file for read/write

An example:

     int ch;
     FILE *input = fopen( "stuff", "r" );
     ch = getc( input );

Related Topics: fclose, fflush, fgetc, fputc, fread, freopen, fseek, fwrite, getc, getchar, setbuf