File I/O
C fopen
C fopen function is used to open existing file or create a new file.
C fopen function is used to open existing file or create a new file.
The basic format of fopen is:
Syntax:
FILE *fopen( const char * filePath, const char * mode );
Parameters
C fopen() access mode can be one of the following values:
Mode | Description |
---|---|
r | Opens an existing text file. |
w | Opens a text file for writing, if file doesn’t exist then a new file is created. |
a | Opens a text file for appending(writing at the end of existing file) and create the file if it does not exist. |
r+ | Opens a text file for reading and writing. |
w+ | Open for reading and writing and create the file if it does not exist. If the file exists then make it blank. |
a+ | Open for reading and appending and create the file if it does not exist. The reading will start from the beginning but writing can only be appended. |
Return Value
C fopen function returns NULL in case of a failure, and returns a FILE stream pointer on success.
Example:
#include<stdio.h>
int main()
{
FILE *fp;
fp = fopen("fileName.txt","w");
return 0;
}
Comments
Post a Comment