File handling is one of the most important concepts in C programming. It allows programs to store data permanently in files instead of keeping it only in memory during program execution.
Using file handling, you can create, read, write, update, append, rename, and delete files. This makes C suitable for developing real-world applications such as student management systems, banking software, inventory management, payroll systems, hospital management systems, and databases. C File Handling Practice questions with solutions help to understand concepts.
C provides several built-in file handling functions through the <stdio.h> library, including:
- fopen() – Opens or creates a file.
- fclose() – Closes an opened file.
- fprintf() – Writes formatted data to a file.
- fscanf() – Reads formatted data from a file.
- fgetc() – Reads one character at a time.
- fputc() – Writes one character at a time.
- fgets() – Reads a complete line.
- fputs() – Writes a complete string.
- remove() – Deletes a file.
- rename() – Renames a file.
In this chapter, you’ll practice practical file handling programs with complete solutions, sample outputs, explanations, and concepts covered.
1. C Program to Create and Write Data to a File
Problem Statement
Write a C program to create a file and write data into it.
C Solution
#include <stdio.h>
int main()
{
FILE *filePointer;
filePointer = fopen("student.txt", "w");
if(filePointer == NULL)
{
printf("Unable to create file.");
return 0;
}
fprintf(filePointer, "Welcome to C File Handling.");
fclose(filePointer);
printf("Data written successfully.");
return 0;
}
Sample Output
Data written successfully.
Explanation
The fopen() function creates the file in write mode (w), fprintf() writes the text, and fclose() saves and closes the file.
Concepts Covered
- File Handling
- fopen()
- fprintf()
- fclose()
2. C Program to Read Data from a File
Problem Statement
Write a C program to read data from a text file.
C Solution
#include <stdio.h>
int main()
{
FILE *filePointer;
char text[100];
filePointer = fopen("student.txt", "r");
if(filePointer == NULL)
{
printf("File not found.");
return 0;
}
fgets(text, sizeof(text), filePointer);
printf("File Content:\n%s", text);
fclose(filePointer);
return 0;
}
Sample Output
File Content:
Welcome to C File Handling.
Explanation
The program opens the file in read mode (r), reads one line using fgets(), and displays it on the screen.
Concepts Covered
- File Reading
- fopen()
- fgets()
- fclose()
3. C Program to Write Multiple Lines to a File
Problem Statement
Write a C program to write multiple lines into a file.
C Solution
#include <stdio.h>
int main()
{
FILE *filePointer;
filePointer = fopen("notes.txt", "w");
if(filePointer == NULL)
{
printf("Unable to open file.");
return 0;
}
fprintf(filePointer, "Line One\n");
fprintf(filePointer, "Line Two\n");
fprintf(filePointer, "Line Three\n");
fclose(filePointer);
printf("Multiple lines written successfully.");
return 0;
}
Sample Output
Multiple lines written successfully.
Explanation
Each call to fprintf() writes a new line into the file.
Concepts Covered
- File Writing
- fprintf()
- Text Files
- File Operations
4. C Program to Append Data to a File
Problem Statement
Write a C program to append new data to an existing file.
C Solution
#include <stdio.h>
int main()
{
FILE *filePointer;
filePointer = fopen("student.txt", "a");
if(filePointer == NULL)
{
printf("Unable to open file.");
return 0;
}
fprintf(filePointer, "\nThis line is added using append mode.");
fclose(filePointer);
printf("Data appended successfully.");
return 0;
}
Sample Output
Data appended successfully.
Explanation
The append mode (a) opens an existing file and writes new data at the end without deleting the existing contents.
Concepts Covered
- File Handling
- Append Mode
- fopen()
- fprintf()
- fclose()
5. C Program to Read a File Character by Character
Problem Statement
Write a C program to read every character from a file using fgetc().
C Solution
#include <stdio.h>
int main()
{
FILE *filePointer;
char character;
filePointer = fopen("student.txt", "r");
if(filePointer == NULL)
{
printf("File not found.");
return 0;
}
printf("File Content:\n");
while((character = fgetc(filePointer)) != EOF)
{
printf("%c", character);
}
fclose(filePointer);
return 0;
}
Sample Output
File Content:
Welcome to C File Handling.
This line is added using append mode.
Explanation
The fgetc() function reads one character at a time until the End Of File (EOF) is reached.
Concepts Covered
- File Reading
- fgetc()
- EOF
- Character Processing
6. C Program to Write Characters to a File Using fputc()
Problem Statement
Write a C program to write characters into a file using the fputc() function.
C Solution
#include <stdio.h>
int main()
{
FILE *filePointer;
filePointer = fopen("characters.txt", "w");
if(filePointer == NULL)
{
printf("Unable to create file.");
return 0;
}
fputc('H', filePointer);
fputc('E', filePointer);
fputc('L', filePointer);
fputc('L', filePointer);
fputc('O', filePointer);
fclose(filePointer);
printf("Characters written successfully.");
return 0;
}
Sample Output
Characters written successfully.
Explanation
The fputc() function writes one character at a time into the specified file.
Concepts Covered
- File Handling
- fputc()
- Character Writing
- Text Files
7. C Program to Read a File Line by Line Using fgets()
Problem Statement
Write a C program to read all lines from a text file using fgets().
C Solution
#include <stdio.h>
int main()
{
FILE *filePointer;
char line[100];
filePointer = fopen("notes.txt", "r");
if(filePointer == NULL)
{
printf("File not found.");
return 0;
}
printf("File Contents:\n\n");
while(fgets(line, sizeof(line), filePointer) != NULL)
{
printf("%s", line);
}
fclose(filePointer);
return 0;
}
Sample Output
File Contents:
Line One
Line Two
Line Three
Explanation
The fgets() function reads one complete line at a time until the end of the file is reached.
Concepts Covered
- fgets()
- File Reading
- Loops
- Text Processing
8. C Program to Write a String to a File Using fputs()
Problem Statement
Write a C program to write a complete string into a file using fputs().
C Solution
#include <stdio.h>
int main()
{
FILE *filePointer;
filePointer = fopen("message.txt", "w");
if(filePointer == NULL)
{
printf("Unable to open file.");
return 0;
}
fputs("Welcome to C Programming File Handling.", filePointer);
fclose(filePointer);
printf("String written successfully.");
return 0;
}
Sample Output
String written successfully.
Explanation
The fputs() function writes an entire string into a text file without formatting.
Concepts Covered
- fputs()
- String Handling
- File Writing
- Text Files
9. C Program to Copy the Contents of One File to Another
Problem Statement
Write a C program to copy all contents from one file into another.
C Solution
#include <stdio.h>
int main()
{
FILE *sourceFile;
FILE *destinationFile;
char character;
sourceFile = fopen("student.txt", "r");
destinationFile = fopen("backup.txt", "w");
if(sourceFile == NULL || destinationFile == NULL)
{
printf("Unable to open files.");
return 0;
}
while((character = fgetc(sourceFile)) != EOF)
{
fputc(character, destinationFile);
}
fclose(sourceFile);
fclose(destinationFile);
printf("File copied successfully.");
return 0;
}
Sample Output
File copied successfully.
Explanation
The program reads one character at a time from the source file and writes it into the destination file until EOF is reached.
Concepts Covered
- File Copy
- fgetc()
- fputc()
- File Handling
10. C Program to Count the Number of Characters in a File
Problem Statement
Write a C program to count the total number of characters present in a text file.
C Solution
#include <stdio.h>
int main()
{
FILE *filePointer;
char character;
int count = 0;
filePointer = fopen("student.txt", "r");
if(filePointer == NULL)
{
printf("File not found.");
return 0;
}
while((character = fgetc(filePointer)) != EOF)
{
count++;
}
fclose(filePointer);
printf("Total Characters = %d", count);
return 0;
}
Sample Output
Total Characters = 62
Note: The actual count depends on the contents of the file.
Explanation
Each character is read individually using fgetc(). A counter is incremented until the end of the file is reached.
Concepts Covered
- Character Counting
- File Reading
- Loops
- EOF
11. C Program to Count the Number of Words in a File
Problem Statement
Write a C program to count the total number of words present in a text file.
C Solution
#include <stdio.h>
int main()
{
FILE *filePointer;
char character;
int wordCount = 0;
filePointer = fopen("student.txt", "r");
if(filePointer == NULL)
{
printf("File not found.");
return 0;
}
while((character = fgetc(filePointer)) != EOF)
{
if(character == ' ' || character == '\n' || character == '\t')
{
wordCount++;
}
}
fclose(filePointer);
printf("Total Words = %d", wordCount + 1);
return 0;
}
Sample Output
Total Words = 10
Note: The exact count depends on the contents of the file.
Explanation
The program counts spaces, tabs, and newline characters to estimate the total number of words in the file.
Concepts Covered
- File Reading
- Word Counting
- Loops
- EOF
12. C Program to Count the Number of Lines in a File
Problem Statement
Write a C program to count the total number of lines in a text file.
C Solution
#include <stdio.h>
int main()
{
FILE *filePointer;
char character;
int lineCount = 0;
filePointer = fopen("student.txt", "r");
if(filePointer == NULL)
{
printf("File not found.");
return 0;
}
while((character = fgetc(filePointer)) != EOF)
{
if(character == '\n')
{
lineCount++;
}
}
fclose(filePointer);
printf("Total Lines = %d", lineCount + 1);
return 0;
}
Sample Output
Total Lines = 3
Explanation
Every newline character (\n) indicates the end of one line. The total number of lines is calculated accordingly.
Concepts Covered
- Line Counting
- File Reading
- Loops
- Character Processing
13. C Program to Rename a File
Problem Statement
Write a C program to rename an existing file.
C Solution
#include <stdio.h>
int main()
{
if(rename("student.txt", "student_record.txt") == 0)
{
printf("File Renamed Successfully.");
}
else
{
printf("Unable to Rename File.");
}
return 0;
}
Sample Output
File Renamed Successfully.
Explanation
The rename() function changes the name of an existing file. It returns 0 when the operation is successful.
Concepts Covered
- rename()
- File Operations
- File Handling
- Error Handling
14. C Program to Delete a File
Problem Statement
Write a C program to delete an existing file.
C Solution
#include <stdio.h>
int main()
{
if(remove("student_record.txt") == 0)
{
printf("File Deleted Successfully.");
}
else
{
printf("Unable to Delete File.");
}
return 0;
}
Sample Output
File Deleted Successfully.
Explanation
The remove() function permanently deletes the specified file from the system.
Concepts Covered
- remove()
- File Deletion
- File Handling
- Error Handling
15. C Program to Demonstrate Different File Modes
Problem Statement
Write a C program to demonstrate commonly used file opening modes in C.
C Solution
#include <stdio.h>
int main()
{
FILE *filePointer;
filePointer = fopen("example.txt", "w");
if(filePointer == NULL)
{
printf("Unable to open file.");
return 0;
}
fprintf(filePointer, "File Handling Example");
fclose(filePointer);
printf("File created using write mode.");
return 0;
}
Common File Modes
| Mode | Description |
|---|---|
r | Open an existing file for reading |
w | Create a new file or overwrite an existing file |
a | Append data to the end of a file |
r+ | Read and write without deleting existing data |
w+ | Read and write after creating a new file |
a+ | Read existing data and append new data |
Sample Output
File created using write mode.
Explanation
Different file modes determine how a program interacts with a file—whether it reads, writes, appends, or updates existing data.
Concepts Covered
- File Modes
- fopen()
- File Operations
- File Handling
Chapter Summary
In this chapter, you learned how to perform file handling in C programming. You practiced creating files, writing and reading data, appending content, copying files, counting characters, words, and lines, renaming files, deleting files, and understanding different file opening modes. These operations are essential for building applications that store and retrieve data permanently.
Key Takeaways
- File handling enables permanent data storage.
fopen()is used to open or create files.fclose()closes a file and saves changes.fprintf()writes formatted data to a file.fscanf()reads formatted data from a file.fgetc()andfputc()handle single characters.fgets()andfputs()work with complete strings or lines.rename()changes a file name.remove()permanently deletes a file.- Understanding file modes (
r,w,a,r+,w+,a+) is essential for efficient file management.
Frequently Asked Questions (FAQs)
1. What is file handling in C?
File handling is the process of creating, reading, writing, updating, renaming, and deleting files using C programming.
2. Which header file is required for file handling?
The <stdio.h> header file provides all standard file handling functions.
3. What is the purpose of fopen()?
fopen() opens an existing file or creates a new one, depending on the selected file mode.
4. What is the difference between w and a modes?
wcreates a new file or overwrites existing content.aappends new data to the end of an existing file without removing old content.
5. Why should fclose() always be used?
fclose() saves any pending changes, releases system resources, and properly closes the file.
6. How do you delete a file in C?
Use the remove() function to permanently delete a file.
7. How do you rename a file in C?
Use the rename() function by specifying the old file name and the new file name.
8. Where is file handling used in real-world applications?
File handling is widely used in student management systems, banking software, payroll systems, hospital management systems, inventory software, log management, configuration files, reporting systems, and database applications, where data needs to be stored permanently.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
