Creating a file In C with Opening and Closing

In this post, we are going to learn about how to Creating a file in C, Opening and Closing a file in C programming. This means we will learn about how to create a file in C. Let’s dive deep.

Create A File:

Let’s see with an example:

#include<stdio.h>
int main()
{
	FILE *fp;
	fp=fopen("abcd.txt","r");
}

Explanation:

Here the above code show how we can create or open a file where we initialize *fp as the file pointer. Then we open the file using the fopen command in a reading mode which is indicated by the word r in inverted commas.

Output:

The code runs without any error and the file which is initialized as abcd.txt gets created in the destination where we have saved our program.

gjWdMDhomb4UZfGy2BPI kKcaZ6dt8XbkUnz6Ey

Join Our WhatsApp Group

Join our WhatsApp Group To know more About Programming Language tips and how to start learning any programming language.

Closing a file in C:

As we have seen to open or create a file in C, let’s see how a file is closed in C.

Syntax:

fclose(file_pointer);

let’s see how to actually use the code.

#include<stdio.h>
int main()
{
	FILE *fp;
	fp=fopen("abcd.txt","r");
	fclose(fp);
}

Explanation:

Here in this code we open the file as we have seen in the above case and follow the same steps and then to close the file we simply add the line fclose with the file pointer as demonstrated above.

Output:

e6K WuI q1pmmvvECp caIt0rU3TZsvfYUyldFT9RVjvL5r028z9HivEqMuxVTD41TvvgjwqB fiReFlGX4HOhS8IZ8EV99tqhDG1WRZ i7vDmx20 clUJus tL9njbdnL8V4Q

Hence the above article demonstrates how to create or open a file and to close a file in C. Hope we were able to clear your doubts.

Default image
Soham Malakar
Hello my name is Soham Malakar am currently pursuing BCA Coding is one of my favorite hobbies. I love to code and an enthusiast to learn different types of programming languages. I love to get intoxicated in the world of technology and video games. A quick learner possessing better results.
Articles: 40

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply