Write a C program to create a file

EX: Write a C program to create a file. How do you create file in file handling. C program to create file.

To learn file handling in C programming first we need to start from creating a file in C. To create a file in C there is a following syntax is used.

Syntax:

FILE *fp;

fp = fopen("file_name","mode");


In above syntax we declared file type pointer  using structure FILE which is already declared in header file stdio.h. to and fopen() function is used to open a file. 


Program to create a file in C:


#include<stdio.h>
int main()
{
    FILE *fp;
   
    fp = fopen("file.txt","w");
/*Close file to release the
allocated memory*/

fclose(fp);
    return 0;
}


Above program will create a file in the same folder where your program file is saved. 
We can also give a path to create a file at specific location.


#include<stdio.h>
int main()
{
    FILE *fp;
   
    fp = fopen("E:file.txt","w");
/*Close file to release the
allocated memory*/

fclose(fp);
return 0;
}


In next exercise we will learn how to write a content in a file.



    Related Articles:


No comments:

Post a Comment

If you have any doubts, please discuss here...👇