C File I/O Overview โ Performing File Input and Output in C
Introduction โ What Is File I/O in C?
File I/O (Input/Output) in C allows programs to interact with external filesโsuch as reading input from a file instead of the keyboard, or writing output to a file instead of the screen. It is part of the Standard I/O Library (<stdio.h>) and enables long-term data storage, report generation, logging, and data transfer.
In this overview, youโll learn:
- How C handles file input and output
- Key file functions and file modes
- Differences between file and standard console I/O
- How to open, read, write, and close files
Core Concept โ The FILE Pointer
C handles files using the built-in FILE data type, which represents a file stream. Before performing I/O, you must associate a file stream with an actual file using the fopen() function.
FILE *fp = fopen("data.txt", "r"); // Open file for reading
fp is a pointer to a file stream. If the file can’t be opened, fopen() returns NULL.
File Opening Modes
You can open files in various modes depending on whether you want to read, write, or append data, and whether the file is text or binary.
| Mode | Description |
|---|---|
"r" | Read from existing text file |
"w" | Write to text file (overwrite if exists) |
"a" | Append to text file |
"r+" | Read & write from existing text file |
"w+" | Read & write to new/overwrite text file |
"a+" | Read & append to text file |
"rb", "wb", "ab" | Binary file equivalents |
Key File I/O Functions in C
| Function | Purpose |
|---|---|
fopen() | Opens a file |
fclose() | Closes a file |
fprintf() | Writes formatted output to file |
fscanf() | Reads formatted input from file |
fgets() | Reads a string/line from file |
fputs() | Writes a string to file |
fread() | Reads binary data |
fwrite() | Writes binary data |
feof() | Checks end of file |
ferror() | Checks file error state |
Writing to a File โ Example
FILE *fp = fopen("log.txt", "w");
fprintf(fp, "Program started\n");
fclose(fp);
Reading from a File โ Example
FILE *fp = fopen("input.txt", "r");
char line[100];
while (fgets(line, sizeof(line), fp)) {
printf("%s", line);
}
fclose(fp);
Validating File Operations
Always check for file open errors:
FILE *fp = fopen("config.txt", "r");
if (fp == NULL) {
perror("Error opening file");
}
Text vs Binary File Modes
- Text mode: human-readable data (
.txt,.csv) - Binary mode: raw byte data (
.bin,.dat)
Choose based on use caseโtext for logs/configs, binary for structured/raw data.
Summary โ Recap & Next Steps
C’s File I/O system allows seamless interaction with files for persistent data handling. Using file streams and standard I/O functions, you can open, process, and manage files safely and efficiently.
Key Takeaways:
- Use
FILE *withfopen()to access files - Choose correct mode (
"r","w","rb", etc.) - Use
fprintf(),fscanf()for text,fwrite(),fread()for binary - Always check if
fopen()succeeded before proceeding - Close files with
fclose()to avoid resource leaks
Real-World Relevance:
Used in report generation, configuration reading, data storage, game saves, system logs, and file parsers.
Frequently Asked Questions (FAQ)
What is a FILE * in C?
Itโs a pointer to a file stream used to read from or write to a file.
How do I open a file in C?
Use fopen("filename.txt", "mode") where mode can be "r", "w", "a", etc.
Whatโs the difference between fscanf and fgets?
fscanf() reads formatted input; fgets() reads a line or string safely.
Why should I close a file after use?
To flush buffers, release system resources, and prevent data loss.
How can I check for end-of-file?
Use feof(FILE *fp) which returns a non-zero value when EOF is reached.
Share Now :
