What is File Handling in C?
A file is nothing more than a method of permanently storing data in the form of a series of bytes on a disc. The contents of a file are not as volatile as the memory of a C compiler. When declaring a file in C, the structure pointer of the file type is used.
The need for file handling in C is because –
- It saves time as we can easily access any part/outcome of the code whenever required.
- It helps in preserving the data or information for reusability.
- There is no need to worry about storage.
- And without loss of data, we can easily transfer the contents of a file.
Types of Files:
When dealing with files, there are two types of files you should know about:
1 . Text files
2 . Binary files
1 . Text Files:-
Text files are the normal .txt files. You can easily create text files using any simple text editors such as Notepad.When you open those files, you’ll see all the contents within the file as plain text. You can easily edit or delete the contents.
2 . Binary Files:-
Binary files are mostly the .bin files in your computer.Instead of storing data in plain text, they store it in the binary form (0’s and 1’s).They can hold a higher amount of data, are not readable easily, and provides better security than text files.
File Operations in C:-
The C programming offers various operations associated with file handling. They are:
- Creating a new file: fopen()
- Opening an existing file in your system: fopen()
- Closing a file: fclose()
- Reading characters from a line: getc()
- Writing characters in a file: putc()
- Reading a set of data from a file: fscanf()
- Writing a set of data in a file: fprintf()
- Reading an integral value from a file: getw()
- Writing an integral value in a file: putw()
- Setting a desired position in the file: fseek()
- Getting the current position in the file: ftell()
- Setting the position at the beginning point: rewind()