<

Open file and write data

Step1: Declare a file pointer. Example:

FILE *fp;

Here FILE is keyword and *fp is pointer of FILE type.
step2: Open file by fopne() library function. Example:

fp=fopen("c:\\test.txt", "w");

fopen() function has two arguments.First is file_path and second is opening_mode. In our case file_path is c:\\test.txt and opening_mode is w. w means writing mode.

Code example:

#include<stdio.h>
int main()
{
    FILE *fp;
    fp=fopen("c:\\test.txt", "w");
    return 0;  
}

When you run this code nothing will be shown to you but a text file named test.txt will be created in your ‘C’ drive.Here fopen(“c:\\test.txt”, “w”); have been assigned to fp so that the file location to write is sent to file pointer fp

Now you can write data into test.txt file by fprintf() function.fprintf(); function has two arguments. First is file pointer and second is data to be written.

#include<stdio.h>
int main()
{
    FILE *fp;
    fp=fopen("c:\\test.txt", "w");
    fprintf(fp, "KUET EEE");
    fclose(fp);
    return 0;  
}

Run this code and it should create a test.txt file in your C drive. Can you see ‘KUET EEE’ inside test.txt file? Hope answer is: YES