Reading and Writing Files in C
Reading and Writing Files in C
Introduction
Reading and Writing Files in C are essential operations used to store and retrieve data permanently. File operations allow programs to save information into files and read it later whenever required.
In this C Programming Course in Jaipur, students learn file reading methods, file writing methods, file functions, text file operations, and practical applications in C programming.
What is File Writing in C?
File writing means storing data into a file permanently.
Data can be written using:
fprintf()fputc()fputs()
Writing files helps save program data for future use.
What is File Reading in C?
File reading means retrieving stored data from a file.
Data can be read using:
fscanf()fgetc()fgets()
Reading files helps access previously saved information.
File Pointer in C
Before file operations:
FILE *fp;
must be declared.
Example:
FILE *fp;
The file pointer manages file access.
Opening File for Writing
Syntax:
fp = fopen("filename", "w");
Mode:
w
creates a new file or overwrites existing content.
Example of Writing Using fprintf()
#include<stdio.h>
int main() {
FILE *fp;
fp = fopen("student.txt", "w");
fprintf(fp, "Welcome to C Programming");
fclose(fp);
return 0;
}
This program writes text into a file.
fprintf() Function in C
The fprintf() function writes formatted output into files.
Syntax:
fprintf(file_pointer, "format", variables);
Example:
fprintf(fp, "%d", number);
Writing Characters Using fputc()
The fputc() function writes a single character into a file.
Syntax:
fputc(character, file_pointer);
Example:
fputc('A', fp);
Example of fputc()
#include<stdio.h>
int main() {
FILE *fp;
fp = fopen("data.txt", "w");
fputc('A', fp);
fclose(fp);
return 0;
}
Writing Strings Using fputs()
The fputs() function writes strings into files.
Syntax:
fputs(string, file_pointer);
Example:
fputs("Hello Student", fp);
Example of fputs()
#include<stdio.h>
int main() {
FILE *fp;
fp = fopen("data.txt", "w");
fputs("Welcome", fp);
fclose(fp);
return 0;
}
Opening File for Reading
Syntax:
fp = fopen("filename", "r");
Mode:
r
opens existing files for reading.
Reading Using fscanf()
The fscanf() function reads formatted data from files.
Syntax:
fscanf(file_pointer, "format", variables);
Example:
fscanf(fp, "%s", name);
Example of fscanf()
#include<stdio.h>
int main() {
FILE *fp;
char text[50];
fp = fopen("student.txt", "r");
fscanf(fp, "%s", text);
printf("%s", text);
fclose(fp);
return 0;
}
Output:
Welcome
Reading Characters Using fgetc()
The fgetc() function reads one character at a time.
Syntax:
fgetc(file_pointer);
Example:
char ch = fgetc(fp);
Example of fgetc()
#include<stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("data.txt", "r");
ch = fgetc(fp);
printf("%c", ch);
fclose(fp);
return 0;
}
Reading Strings Using fgets()
The fgets() function reads entire lines including spaces.
Syntax:
fgets(string, size, file_pointer);
Example:
fgets(text, 50, fp);
Example of fgets()
#include<stdio.h>
int main() {
FILE *fp;
char text[50];
fp = fopen("data.txt", "r");
fgets(text, 50, fp);
printf("%s", text);
fclose(fp);
return 0;
}
End of File (EOF)
EOF means:
End of file reached
Example:
while((ch = fgetc(fp)) != EOF)
EOF is important during file reading loops.
Append Mode in File Handling
Mode:
a
adds new content without deleting old content.
Example:
fp = fopen("data.txt", "a");
Applications of File Reading and Writing
File operations are used in:
- Banking systems
- Student management systems
- Billing applications
- Report generation
- Inventory software
- Database systems
Students learning C Programming Course in Jaipur use file operations extensively in practical projects.
Advantages of File Handling
Benefits:
- Permanent data storage
- Large data management
- Easy information retrieval
- Supports real-world applications
File handling is essential for software systems.
Common Errors in File Handling
File Not Found
Opening non-existing file in read mode may fail.
Forgetting fclose()
Files should always be closed properly.
NULL File Pointer
Incorrect:
fprintf(fp, "Hello");
when:
fp == NULL
This may crash the program.
Incorrect File Modes
Using wrong file mode may overwrite data accidentally.
Best Practices
- Always check file opening success
- Close files after operations
- Use proper file modes
- Validate file paths
- Handle errors carefully
Good file management improves reliability and security.
Importance of Reading and Writing Files in C
File operations help:
- Store permanent records
- Build database-style systems
- Process large datasets
- Create real-world applications
Understanding file operations is essential for advanced programming.
Summary
Reading and Writing Files in C help programs store and retrieve data permanently using file operations. Functions like fprintf(), fscanf(), fputc(), fgetc(), fputs(), and fgets() simplify file management.
This lesson explained file reading, writing, append mode, file functions, applications, common errors, and best practices in C programming.
FAQs
What is file writing in C?
File writing stores data permanently into files.
What is file reading in C?
File reading retrieves stored data from files.
What does fprintf() do?
fprintf() writes formatted data into files.
Why is EOF important?
EOF indicates the end of a file during reading operations.
Where is file handling used?
File handling is used in databases, banking systems, report generation, and management software.
