Learn Input Output operations in File Handling in C using fprintf(), fscanf(), fputc(), and fgetc(). Understand file read/write with examples and syntax for beginners.
Introduction
In C programming, files play a crucial role in storing data permanently.
Unlike normal input-output operations using scanf() and printf(), file I/O allows you to read from and write to external files, enabling persistent data storage.
These operations are performed using standard I/O library functions such as:
fprintf()fscanf()fputc()fgetc()fputs()fgets()
Let’s explore each of them with examples, syntax, and practical use cases.
Types of File Input and Output Functions
1. Writing Data to a File (Output Operations)
Output operations involve sending data to a file — either as characters, strings, or formatted text.
✅ Using fprintf()
fprintf() is used to write formatted data into a file, similar to printf() but targets a file instead of the console.
Syntax:
fprintf(file_pointer, "format_string", variables);Example:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("student.txt", "w");
if (fp == NULL) {
printf("File opening failed!");
return 1;
}
fprintf(fp, "Name: John Doe\nAge: %d\n", 20);
fclose(fp);
printf("Data written successfully.\n");
return 0;
}Explanation:
- The file student.txt is opened in write mode.
- Data is written using
fprintf(). - The file is closed with
fclose().
✅ Using fputc()
fputc() writes a single character to a file.
Syntax:
fputc(character, file_pointer);Example:
fputc('A', fp);✅ Using fputs()
fputs() writes a string to a file.
Syntax:
fputs("Hello World", fp);Example:
#include <stdio.h>
int main() {
FILE *fp = fopen("output.txt", "w");
fputs("Learning File I/O in C", fp);
fclose(fp);
return 0;
}2. Reading Data from a File (Input Operations)
Input operations involve reading data from a file for processing.
✅ Using fscanf()
fscanf() reads formatted data from a file, similar to scanf().
Syntax:
fscanf(file_pointer, "format_string", variables);Example:
#include <stdio.h>
int main() {
FILE *fp;
char name[50];
int age;
fp = fopen("student.txt", "r");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
fscanf(fp, "Name: %s\nAge: %d", name, &age);
printf("Name: %s\nAge: %d\n", name, age);
fclose(fp);
return 0;
}✅ Using fgetc()
fgetc() reads a single character from a file.
Syntax:
character = fgetc(file_pointer);Example:
char ch;
while ((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
}✅ Using fgets()
fgets() reads a string from a file until a newline or end-of-file.
Syntax:
fgets(string_variable, size, file_pointer);Example:
char line[100];
fgets(line, 100, fp);
printf("%s", line);Example: Complete File I/O Program in C
#include <stdio.h>
int main() {
FILE *fp;
char name[30];
int age;
// Writing to file
fp = fopen("data.txt", "w");
fprintf(fp, "Alice 25\nBob 30\n");
fclose(fp);
// Reading from file
fp = fopen("data.txt", "r");
while (fscanf(fp, "%s %d", name, &age) != EOF) {
printf("Name: %s, Age: %d\n", name, age);
}
fclose(fp);
return 0;
}Output:
Name: Alice, Age: 25
Name: Bob, Age: 30Commonly Used File I/O Functions in C
| Function | Purpose |
|---|---|
fopen() | Opens a file in specified mode |
fclose() | Closes the file |
fprintf() | Writes formatted data to file |
fscanf() | Reads formatted data from file |
fputc() | Writes a single character |
fgetc() | Reads a single character |
fputs() | Writes a string |
fgets() | Reads a string |
Importance of File Input/Output in C
- Allows data persistence beyond program execution.
- Enables report generation and logging.
- Facilitates data exchange between programs.
- Crucial for database, text processing, and embedded systems applications.
Text vs Binary File I/O
| File Type | Description | Example Functions |
|---|---|---|
| Text File | Stores data as readable text | fprintf(), fscanf() |
| Binary File | Stores data in machine-readable format | fwrite(), fread() |
Best Practices for File I/O in C
✅ Always check if fopen() returns NULL.
✅ Always close files using fclose().
✅ Use correct file modes (r, w, a, etc.).
✅ Flush data before program exits (fflush(fp)).
✅ Avoid reading/writing outside buffer limits.
Conclusion
The Input and Output operations in file in C are powerful tools that allow programs to store, retrieve, and manage data efficiently.
By mastering functions like fprintf(), fscanf(), fputc(), and fgetc(), you can handle both text and binary data seamlessly in C programming.
Whether you’re developing a data logger, text editor, or system-level application — file I/O operations are essential for real-world C programming.
