C File Handling

We have learned lots of things in c programming, accept user input, execute, and print results on the terminal(output) window. But results are not store permanently, to store data for future reference we are using file handling in c language. Now, instead of accepting input and printing results from the terminal, we will use a file in c to accept input and store results. Accepting input from the file is called reading and printing output in a file is called writing in c language.

What is a File?

The File is a collection of bytes, and it is permanently stored in computer’s secondary memory. There are two types of file in c.

  1. Text File: Text files are normal .txt files, that store information in ASCII format. The user can directly open that file and can read easily.
  2. Binary File: Binary files store information in the binary format (0 or 1). Not user readable.

Basic File Operation in C Language.

When you are working with the file in c, you need to declare a pointer variable of FILE type to store file reference during reading or write operation. Below you can check basic file operation in c.

  • Open or create a File.
  • Read or Write a File.
  • Close the File.

Example 1: C program to write user input in a File.

#include<stdio.h>
int main()
{
FILE *fp;	//declaring a file pointer to store file reference.
char month[20];
	fp=fopen("first.txt","w");	// opening a file in write mode.
	printf("\nEnter Month: ");
	scanf("%s",month);
	fprintf(fp,"%s",month);	//writing text in a file.
	fclose(fp);	//closinfg file.
return 0;
}

FILE *fp – Here we are declaring a pointer variable of FILE type to store file reference in c programming.

fp=fopen(“first.txt”,”w”) – To open a file for read/write fopen(“file”,”mode”) function is used.

fprintf(fp,”%s”,weekday) – To write formated output in file, fprintf(file_pointer,”format”,text) function is used.

fclose(fp) – Every file must be close after read or write operation using fclose() function in c.

So, in the above c code first, we create a file pointer, open a file to write, after that write string into a file, and finally close the file.

Example 2: C program to read text from a File.

#include<stdio.h>
int main()
{
FILE *fp;	//declaring a file pointer to store file reference.
char month[20];
	fp=fopen("first.txt","r");	// opening file in read mode.
	fscanf(fp,"%s",month);	//reading from file.
	printf("\nMonth : %s",month);
	fclose(fp);	//closinfg file.
return 0;
}

Mini Quiz

0%

C Programming - Quiz 17

1 / 5

Q1. What is the main purpose of file handling in C?

2 / 5

Q2. Which function is used to open a file in C?

3 / 5

Q3. Which mode is used to open a file for writing?

4 / 5

Q4. Which function is used to write formatted data to a file?

5 / 5

Q5. Why is fclose() used in C file handling?

Your score is

The average score is 0%

0%

Mini Project

Mini Project: City Information

Question:

Write a C program that accepts the name of your favorite city from the user and stores it in a text file named city.txt. After storing the city name, read the data from the same file and display it on the screen.

View Answer Code
#include<stdio.h>

int main()
{
    FILE *fp;
    char city[30];

    fp = fopen("city.txt", "w");

    printf("Enter Your Favorite City: ");
    scanf("%s", city);

    fprintf(fp, "%s", city);

    fclose(fp);

    fp = fopen("city.txt", "r");

    fscanf(fp, "%s", city);

    printf("\nFavorite City: %s", city);

    fclose(fp);

    return 0;
}

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top