Introduction to File Handling
Learn about file pointers and how to open, read, write, and close files using fopen, fclose, fprintf, and fscanf.
Introduction to File Handling is a free C Academy lesson on CoddyKit. This is lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 3 lessons in total.
Storing Data with Files
Introduction to File Handling
File handling allows programs to store and retrieve data persistently by reading from and writing to files.
In this lesson, you will learn:
- How to open and close files in C.
- How to read and write data using file handling functions.
- The importance of file operations in real-world applications.

Opening a File
Files in C are opened using the fopen() function.
Syntax:
FILE *filePtr = fopen("filename.txt", "mode");
Modes:
r- Readw- Writea- Append
Closing a File
Files should be closed using the fclose() function to free resources.
Example:
fclose(filePtr);
Failing to close files may cause memory leaks or file corruption.
Writing to a File
The fprintf() function writes formatted data to a file.
Example:
fprintf(filePtr, "Hello, World!\n");
This writes Hello, World! to the file.
Example: Writing to a File
This program demonstrates how to write to a file.
#include <stdio.h>
int main() {
FILE *filePtr = fopen("output.txt", "w");
if (filePtr == NULL) {
printf("Error opening file!\n");
return 1;
}
fprintf(filePtr, "Hello, File Handling in C!\n");
fclose(filePtr);
return 0;
}Reading from a File
The fscanf() function reads formatted data from a file.
Example:
fscanf(filePtr, "%s", str);
This reads a string from the file into str.
Example: Reading from a File
This program demonstrates how to read data from a file.
#include <stdio.h>
int main() {
FILE *filePtr = fopen("output.txt", "r");
if (filePtr == NULL) {
printf("Error opening file!\n");
return 1;
}
char str[50];
fscanf(filePtr, "%s", str);
printf("File Content: %s\n", str);
fclose(filePtr);
return 0;
}Appending to a File
The a mode appends data to an existing file.
Example:
FILE *filePtr = fopen("data.txt", "a");
This opens data.txt for appending new content.
Summary
In this lesson, you learned:
- How to open and close files.
- How to write and read data using
fprintfandfscanf. - How to append data to an existing file.
Next, we will explore working with binary files in C!

Learn C with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 39
- Lessons
- 144
Frequently Asked Questions
Is the “Introduction to File Handling” lesson free?
Yes — the full text of “Introduction to File Handling” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 3 lessons in total.
What will I learn in “Introduction to File Handling”?
Learn about file pointers and how to open, read, write, and close files using fopen, fclose, fprintf, and fscanf. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.
How long does the “Introduction to File Handling” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to File Handling
- Working with Binary Files
- File Error Handling