This may help you to understand your problem
fopen function
Purpose:
Opens a stream. The safer fopen_s function is also available.
Syntax:
FILE * fopen(const char *name, const char *mode);
Declared in:
The fopen function opens the file whose name is the string pointed to by name, and associates a stream with it. The string may contain a full path (from the root), a relative path (from the current directory) or just a name.
The argument mode points to a string. If the string is one of the following, the file is open in the indicated mode.
- "r" Open text file for reading.
- "w" Truncate to zero length or create text file for writing.
- "a" Append; open or create text file for writing at end-of-file.
- "rb" Open binary file for reading.
- "wb" Truncate to zero length or create binary file for writing.
- "ab" Append; open or create binary file for writing at end-of-file.
- "r+" Open text file for update (reading and writing).
- "w+" Truncate to zero length or create text file for update.
- "a+" Append; open or create text file for update, writing at end-of-file.
- "rb+" Open binary file for update (reading and writing).
- "wb+" Truncate to zero length or create binary file for update.
- "ab+" Append; open or create binary file for update, writing at end-of-file.
- "r+b" Same as "rb+"
- "w+b" Same as "wb+"
- "a+b" Same as "ab+"
Opening a file with read mode ('r' as the first character in the mode argument) fails if the file does not exist or cannot be read.
Opening a file with append mode ('a' as the first character in the mode argument) causes all subsequent writes to the file to be forced to the then current end-of-file, regardless of intervening calls to the fseek function.
When a file is opened with update mode ('+' as the second or third character in the mode argument), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file.
When opened, a stream is fully buffered if and only of it can be determined not to refer to an interactive device. The error and end-of-file indicators for the stream are cleared.
Returns:
A pointer to the object controlling the stream on success, otherwise a null pointer.