File Handling in C Programming Language

File:- In this chapter, we are going to study data files. A file is a collection of related records such as the records of employees in a company. Answer Each record will be a collection of related data items called fields. Each field consists of group of characters.
The common operations associated with the processing of data files:
  • Reading from a file (input) 
  • Writing to a file (output) 
  • Appending to a file (writing at the end) Updating a file (input or output I/O)
These files can be subdivided into two categories:- 
1. Text file:- A text file stores textual information like alphabets, numbers, special symbols, etc. In actuality, the ASCII code of textual characters is stored in the text files.
They consist of consecutive characters. These characters can be interpreted as individual data items, or as components of strings or numbers.
Read Also:
  1. Strings
  2. Structures
  3. Union
Since data is stored in a storage device in the binary format the text file contents are first converted in the binary form before actually being stored in the storage device. A text file can store different character sets such as:
  • Upper case English alphabets (A to Z) 
  • Lower case English alphabets (a to z)
  • Numeric characters (like 1, 3, 5, etc.)
  • Punetuation characters (like :,;,, ?, etc.)
  • Special characters (like $. %, etc.)
2. Binary file:- A binary file stores the information in the binary form, i.e., in the same format as it is stored in the memory. Thus, the use of binary file eliminates the need of data conversion from text to binary format for storage purpose. One of the main drawbacks of binary file is that the data stored in a binary file is not in human understandable form. C language supports binary file operations such as read, write and append. 
File Handling Functions:- There are many file handling functions, which used for handling a file or more files.
The various file handling functions is written below:
gete(): gete() function is used to read a single character from a given file and returns EOF(End-of-file) condition at the end or if an error occurs. The general format of the getc() function is:

where fptr is a file pointer of the file, and ch is a variable that receives the character. fgete(): fgete() is used to read a single character from a given file. The general format of the fgetc() is:

where fptr is a file pointer of the file, and ch is a variable that receives the character. putc(): The putc() funetion is used to write a single character into a file. The general format of the putc() function is:

where ch is the character to be written and fptr is a file pointer to a file to receive a character.
fputc(): The fputc function is used to write a single character on to a given file and advance the associated file position indicator.
The general syntax of the fpute () is:

where ch is a character to be written and fptr is a file pointer to the file to receive the character.
fputs(): The fputs () is used to write a given string to a given file . It is normally used to copy strings from one file to another. The general format of the fputs function is as follows:
where sptr is a pointer to the string to be written and fptr is a file pointer to the file. fgets(): fgets() function reads characters until it reaches a new line, an-end-of-file , or the maximum number of characters specified
The format of fgets() function is:

fread(): The fread() function is used to read a block of binary data from a given file. The general format of the fread() function is:
fwrite(): The fwrite() function is used to write elements from the array to the stream.
The general format of fwrite () is:

fscanf( ): fscanf () function is used to read formatted data from a specified file.
The general syntax of fscanf() function is:
fprintf(): fprintf() function is used to write formatted data into a given file.
The general form of fprintf() function is: where fptr is a file pointer to write formatted data.
getw(): getw() function is used to read an integer value from a given file.
The general format of getw () is:

where fptr is a pointer to afile to receive an integer value.
putw(): putw() function is used to write an integer quantity on to the specified file. The general format of putw() function is:

where w is an integer value to be written and fptr is a file pointer to a given file.
Accessing Of File:- For accessing a file we need some Mode for accessing a file, there are following Mode for accessing a file:
  1. "r" = To open a file for reading only (for input). Reading occurs from the beginning.
  2. "w" = To open a file for writing only (for output). If the file already exists, it will be overwritten.
  3. "a" = To open a file for appending (for output). We can write only at the end-of-file position. Even if we explicitly move the file indicator, writing shall occur at the end-of-file. 
  4. "r+" = To open an existing file for reading and writing (I/0). The file position indicator is initially set to the beginning of the file. 
  5. "w+" =  To open a new file for reading and writing (I/O). If the file already exists, its contents are destroyed. If the file does not exist, a new file is created. 
  6. "a+" = To open a file for reading and appending (I/O) and create the file if it does not exist. We can read data anywhere in the file, but we can write data only at the end-of-file. 
  7. "rb" = Open binary file for reading. 
  8. "wb" = Create binary file for writing. 
  9. "ab" = Append to a binary file.
  10. "rb" or "r+b" = Open binary file for read/write. 
  11. "wb" or "w+b" = Create binary file for read/write.

Post a comment

2 Comments