Writing to Files
1. Introduction
Writing to files in C means storing data in a file so that it can be used later. Unlike variables, file data is stored permanently on secondary storage such as a hard disk. File writing is very useful when a program needs to save results, records, or reports.
Writing to files is widely used in:
-
Student and employee record systems
-
Billing and accounting software
-
Log files and reports
-
Database applications
C provides several built-in functions to write data into files efficiently.
2. Steps to Write to a File in C
The following steps are followed to write data to a file:
-
Declare a file pointer
-
Open the file in write or append mode
-
Write data using file writing functions
-
Close the file
3. File Pointer
A file pointer is required to connect the program with a file.
Declaration
-
FILEis a structure defined instdio.h -
fpstores information about the file
4. Opening a File for Writing
To write data into a file, we use the fopen() function.
Syntax
Common Writing Modes
| Mode | Description |
|---|---|
"w" |
Write mode (creates new file / deletes old data) |
"a" |
Append mode (adds data at the end) |
"w+" |
Read and write |
"a+" |
Read and append |
Example
âš Important:
-
"w"mode erases old data -
"a"mode preserves old data
5. Writing Data Using fprintf()
Description
-
Writes formatted output to a file
-
Works like
printf()but writes to a file
Syntax
Example Program
Use
-
Best for writing text and formatted data
6. Writing Data Using fputc()
Description
-
Writes one character at a time
Syntax
Example
Use
-
Character-by-character writing
7. Writing Data Using fputs()
Description
-
Writes a string into a file
Syntax
Example Program
Advantage
-
Simple and fast for strings
8. Writing Multiple Lines to a File
Example Program
9. Writing Data Using Append Mode
Append mode adds data at the end of the file.
Example
✔ Old data remains safe
✔ New data is added at the end
10. Writing to Binary Files
Binary files store data in binary format, not readable by humans.
Function Used: fwrite()
Syntax
Example: Writing a Structure to a Binary File
11. Importance of Closing a File
After writing data, the file must be closed using fclose().
Syntax
Reasons
-
Saves data properly
-
Frees memory
-
Prevents data loss
12. Comparison of File Writing Functions
| Function | Writes | Best For |
|---|---|---|
fprintf() |
Formatted text | Text files |
fputc() |
Single character | Character writing |
fputs() |
String | Line writing |
fwrite() |
Binary data | Binary files |
13. Common Errors While Writing to Files
-
Opening file in wrong mode
-
Forgetting to close the file
-
Using invalid file pointer
-
Overwriting data accidentally
14. Advantages of Writing to Files
-
Permanent storage of data
-
Useful for large data handling
-
Data can be reused later
-
Helps in creating reports and records
15. Conclusion
Writing to files in C allows programs to store data permanently. By using file pointers and file writing functions like fprintf(), fputc(), fputs(), and fwrite(), data can be stored efficiently in both text and binary files. Understanding file writing is essential for developing real-world C applications.