The fopen() function defined in the stdio.h header file. It helps to open the file specified in the argument filename in a given mode.
FILE *fopen(const char *filename, const char *mode); #where filename should be a string
The fopen() function takes two parameters. The file open modes are the following types.
Parameter | Description | Required / Optional |
---|---|---|
filename | the name of the file to be opened | Required |
mode | the file access modes | Required |
The return value of fopen() function is a file pointer. Else it returns the NULL, and for indicating the error global variable errno is set.
Input | Return Value |
---|---|
if execution succeeds | file pointer |
if execution not succeed | NULL |
#include <stdio.h>
int main () {
FILE * pnt;
pnt = fopen ("myfile.txt", "w+");
fprintf(pnt, "%s %s %s %d", "Hii", "all", "I am", 2000);
fclose(pnt);
return(0);
}
Output:
Hii all I am 2000
#include <stdio.h>
int main()
{
// pointer to FILE
FILE* pnt;
pnt = fopen("testfile.txt", "w+");
// adds content to the file
fprintf(pnt, "%s %s %s", "C Program",
"tutorials");
// closes the file
fclose(pnt);
return 0;
}
Output:
C Program tutorials