๐ 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 :