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:
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:
Read Also:
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:
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:
- "r" = To open a file for reading only (for input). Reading occurs from the beginning.
- "w" = To open a file for writing only (for output). If the file already exists, it will be overwritten.
- "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.
- "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.
- "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.
- "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.
- "rb" = Open binary file for reading.
- "wb" = Create binary file for writing.
- "ab" = Append to a binary file.
- "rb" or "r+b" = Open binary file for read/write.
- "wb" or "w+b" = Create binary file for read/write.
1 Comments
Nice
ReplyDelete