fopen function

Prototype FILE * fopen (const char * FileName, const char * Mode);
PurposeTo open a file for input or output.
Header File#include <stdio.h>
Parameters
FileNameOperating System name for the FILE to be opened
Mode
rOpen for reading (existing file only)
rbOpen for reading (existing file only) in binary mode
rtOpen for reading (existing file only) in text mode
r+Open for update (existing file only)
rb+Open for update (existing file only) in binary mode
rt+Open for update (existing file only) in text mode
wOpen (or create) for writing (and delete any previous data)
wbOpen (or create) for writing (and delete any previous data) in binary mode
wtOpen (or create) for writing (and delete any previous data) in text mode
w+Open (or create) for update (and delete any previous data)
wb+Open (or create) for update (and delete any previous data) in binary mode
wt+Open (or create) for update (and delete any previous data) in text mode
aOpen (or create) for append (and keep any previous data)
abOpen (or create) for append (and keep any previous data) in binary mode
atOpen (or create) for append (and keep any previous data) in text mode
a+Open (or create) for append update (and keep any previous data)
ab+Open (or create) for append update (and keep any previous data) in binary mode
at+Open (or create) for append update (and keep any previous data) in text mode
Notes:
  • In binary mode (b), data values (including CR, LF, and EOF) are passed without translation.
  • In text mode (t), with DOS:
    • A CR/LF pair from the file is translated to '\n' by a read.
    • A '\n' is translated to a CR/LF pair into the file by a write.
    • An EOF byte from the file is not passed as data by a read; instead it sets the EOF flag.
  • If neither binary or text mode is specified, the mode is determined by the value of the global variable _fmode.
    • If _fmode is equal to O_BINARY, the default mode for fopen is binary.
    • If _fmode is equal to O_TEXT, the default mode for fopen is text.
Return Value
Valid pointer to FILESuccess
NULLFailure
Notes
  • If a file is opened for update, both reads and writes are allowed; however, they must be separated from each other by an intervening seek or rewind.
Example #include <stdio.h> . . . FILE * Account_Master; char * Account_Master_Name; . . . strcpy (Account_Master, "E:\DATA\ACCOUNTS.DAT"); Account_Master = fopen (Account_Master_Name, "ab+"); if (Account_Master == 0) printf ("Open failed."); else printf ("Open succeeded.");
Related functions
fclose fread fwrite fseek rewind ftell

c/c++ Index