fopen
The fopen function opens a file in the file system and returns a pointer to a FILE structure, which can be used later for further read/write access.
The fopen function requires two arguments. The first argument is the name of the file to be opened, given as a null terminated string. The second argument is also a string, but it encode the type of access to the file. For example:
- "r": means that the file will be opened only for reading.
- "w": means that the file will be opened only for writing.
- "a": means that the file will be only for writing at the end of the existing content, that is, for appending.
- "rw": means that the file will be only for both reading and writing.
The return value of the fopen function is a pointer to a FILE structure. The value of this pointer is NULL if there was any problem while opening the file. Otherwise, the pointer is for a valid FILE structure that can be used in further accesses to the file.
In normal circumstances, every call to fopen should correspond to a call to fclose. However, when a program ends all open files are closed automatically by the operating system, so it is not necessary to call fclose if this is the last step of a program.
Post a comment