C – File Handling

In this lesson, let us understand how to handle files in C. We will cover the following concepts:

  • What is File Handling
  • Create and close a file
  • Write to a File
  • Read a File

What is File Handling

File Handling in C includes creating, reading, and writing a file. It also includes deleting a file. To work on files in C programming, we have the fopen() method which is a part of the <stdio.h> header file. To use it, declare a pointer of data type FILE.

Before that, let us first see the syntax of the fopen() method:

Here, file_name is the name of the file you want to create.
The mode is the read mode, write, and append. Only a single character represents the mode:

  • r – read a file
  • w – write to a file
  • a – append to a file

We will create the following file pointer. Here, *fp is a pointer. FILE is a data type defined in the standard I/O library stdio.h that represents a file:

The * i.e., the asterisk is a pointer that indicates the fp is a pointer to a FILE object. Here, fp is the name of the file pointer variable.

For more on pointers: Pointers in C

Create and close a File

To create a file in C programming, use the fopen() method and the mode w. Always remember to close an opened file using fclose() method.

Let’s say the name of the file we want to create is myfile.txt at the following location. Use the double backslash in VS Code (Windows) for the path as shown below:

Let us now see an example to create a file in C:

The file myfile.txt gets created at the location. The file is empty since we did not add any text to it:
file created in C

Write to a File

To write to a file in C, use the fprintf() method, but before that, open the file in w mode like we saw above. Set the pointer in fprintf() and the text you want to write to the new file myfile2.txt.

We are creating a new file myfile2.txt in the same folder.

Let us now see the C program to write text to the file myfile2.txt:

The following will be output, i.e., the file written successfully. The myfile2.txt file has the following text as shown below:
file written in C

Read a File

To read a file in C Programming, use the fopen() method and the mode r. Always remember to close an opened file using fclose() method.

We will read the above myfile2.txt with the following text:

Read a file in C

The fgets method will be used to read a string from a file. It reads up to n-1  characters from the fils stream and stores them in to the buffer pointed to by str. Here is the syntax:

Here,

  • str is the pointer to the array where the read string will be stored.
  • n is the maximum number of characters to read (including the null terminator).
  • stream is the pointer to the file from which you want to read.

Let us now see the example to read the above file i.e. myfile2.txt that already exists:

Output


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

C - Pointers
C Programming Cheat Sheet
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading