Translations of this page?:

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.

ModeMeaningAlready ExistsDoes Not Exist
“r”Open a file for readingread from starterror
“w”Create a file for writingdestroy contentscreate new
“a”Append to a filewrite to endcreate new
“r+“Open a file for read/writeread from starterror
“w+“Create a file for read/writedestroy contentscreate new
“a+“Open a file for read/writewrite to endcreate new

You can also optionally specify the “b” flag to open a file in binary mode on Windows systems. This flag is ignored on POSIX systems (including Linux), so it's safe to always specify this flag when operating on binary files.

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

 
• • • IndexRecent changesRSScc