4.4 File Management in C
Introduction to File Operations in C
- In C programming, files are used to store data permanently on the disk.
- Unlike variables, data in files is not lost when the program ends.
- File operations allow reading, writing, and modifying data stored in files.
- Creating a file
- Opening a file
- Reading from a file
- Writing to a file
- Closing a file
1) Creating a File: -
- In C, a file is created using the fopen( ) function.
- The fopen( ) function requires two arguments: the file name and the mode.
- To create a new file, we usually use the mode "w" (write) or "a" (append).
- If the file does not exist, "w" will create it. If it exists, "w" will overwrite it.
Example: -
Output: -
2) Opening a File: -
- To access a file, you must first open it using fopen( ).
- Different modes can be used:
"r" → read mode
"w" → write mode
"a" → append mode
"r+" → read and write mode
"w+" → write and read mode (overwrites file)
- Always check if the file was opened successfully by checking if the pointer is NULL.
Output: -
3) Reading from a File: -
- We can read data from a file using functions like fgetc( ), fgets( ), or fscanf( ).
- fgetc( ) reads one character at a time, fgets( ) reads a line, and fscanf( ) reads formatted data.
Example using fgets( ):
-
Output: -
4) Writing to a File: -
- We can write data using fputc( ), fputs( ), or fprintf( ).
- fputc( ) writes one character, fputs( ) writes a string, and fprintf( ) writes formatted data.
- Writing to a file in "w" mode overwrites the existing content.
Example using fputs( ): -
Output: -
5) Closing a File: -
- After performing file operations, always close the file using fclose( ).
- Closing a file frees up system resources and ensures data is properly saved.
Example: -
Output: -
Working with text and binary files: -
- In C programming, files are used to store data permanently. Based on how data is stored, files are mainly categorized into text files and binary files.
1) Text Files: -
- Text files store data in human-readable format, i.e., as plain characters.
- Each line of a text file ends with a newline character (\n).
- Text files are ideal for small amounts of data that can be read or edited manually.
- Standard functions for text file operations include: fopen( ), fclose( ), fprintf( ), fscanf( ), fgets( ), fputs( ), fgetc( ), and fputc( ).
Advantages of Text Files
- Human-readable and easy to edit.
- Portable across different platforms.
- Suitable for small data storage.
2) Binary Files: -
- Binary files store data in machine-readable format (binary), not as plain text.
- They are more efficient and faster for large data storage.
- Cannot be read or edited directly with a text editor.
- Functions used: fread( ), fwrite( ), fseek( ), ftell( ), and fclose( ).
"wb" → Write in binary mode.
"rb" → Read in binary mode.
"ab" → Append in binary mode.
Advantages of Binary Files
- Efficient storage, especially for large datasets.
- Faster reading and writing.
- Exact data representation (important for structures, arrays, etc.).
|
Feature |
Text File |
Binary File |
|
Format |
Human-readable |
Machine-readable |
|
Size |
Larger (due to text) |
Smaller |
|
Efficiency |
Slower for large data |
Faster |
|
Editing |
Can be edited manually |
Cannot edit manually |
|
Data Loss Risk |
Newlines may cause issues |
Exact data preserved |
Example: -
Output: -