Open and close file in C by R4R Team

Open File:
We use fopen() function to open the existing file.
-If file is not exist then fopen() automatically create that file.

Syntax-
fp=fopen("Filename","mode");
Where,
fp is the file pointer.

Close file:
Syntax-
fclose(fp);

Create File pointer:
Syntax-
FILE *fp;

Example-
fp=fopen("Filename.txt","r"); //open file in read mode
fp=fopen("Filename.txt","w"); //open file in write mode
fp=fopen("Filename.txt","a"); //open file in append mode
fp=fopen("Filename.txt","r+"); //open file in read and write mode
fp=fopen("Filename.txt","w+"); //open file in read and write mode


program-

#include< stdio.h>
void main()
{
FILE *fp;
fp=fopen("Myfile.txt","r"); //open file in read mode
printf("Your file is createdn");
fclose(fp); // close file
printf("Your file is closed");
}


output-

Your file is created
Your file is created

-In this program, we do not perform any task on file, we just try to open and close the file.

Goto next part , i.e., Write in Files-




Leave a Comment: