Open file and write data

Step1: Declare a file pointer. Example:

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

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:

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.

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


Next Previous