Basics of File Handling in C Programming
File Handling in C programming involve various tasks related to handling files, such as creating, opening, reading, writing, and closing files. These operations are performed using functions provided by the C Standard Library, such as fopen, fclose, fread, and fwrite. Here are details on these operations with examples:
Opening a File (fopen)
To open a file, we use the fopen() function. This function takes two arguments: the name of the file to open and the mode in which to open it. The mode can be one of the following:
- r: Open the file for reading.
- w: Open the file for writing. If the file does not exist, it will be created. If the file already exists, it will be truncated.
- a: Open the file for appending. If the file does not exist, it will be created. If the file already exists, the new data will be appended to the end of the file.
Example of opening a file for writing:
Closing a File (fclose)
The fclose function is used to close a file when you're done with it. This is important for releasing system resources and ensuring data is written if it's a write operation.
Example of closing a file:
Reading from a File (fread)
The fread function is used to read binary data from a file. It takes the destination memory location, the size of each element to read, the number of elements to read, and the file pointer.
Example of reading binary data:
Writing to a File (fwrite)
The fwrite function is used to write binary data to a file. It takes the source memory location, the size of each element to write, the number of elements to write, and the file pointer.
Example of writing binary data:
Checking for the End of a File (feof)
The feof function is used to check whether the end of the file has been reached. It returns a non-zero value if the end of the file has been reached.
Example of checking for the end of a file in a loop:
Other file operations
In addition to the basic operations of opening, reading, writing, and closing files, there are a number of other file operations that can be performed in C programming. These include:
- fseek(): This function sets the file pointer to a specific position in the file.
- ftell(): This function returns the current position of the file pointer in the file.
- rewind(): This function rewinds the file pointer to the beginning of the file.
- remove(): This function deletes a file.
- rename(): This function renames a file.
Conclusion
File operations in C are essential for handling data stored in files, and they enable the reading and writing of data to and from files in both text and binary formats. Proper error handling, as demonstrated in the examples, is essential to handle situations where file operations might fail due to issues like file not found or permission errors.
- Reading from a file in C progrmming
- Writing to a file in C programming
- Detecting EOF in C programming
- File Pointer in C
- File modes in C programming
- Reading and Writing Text Files in C
- Reading and Writing to Binary Files in C
- Random Access File in C
- The Difference Between Text File and Binary File
- Error handling during file operations in C
- File locking in C program
- Copying, Renaming and Deleting files in C
- Working with Directories in C programming