File Handling
Stream
- A logical interface to a file is called Stream.
- A sequence of characters from input device is called Input Stream.
- A sequence from computer to output device is called Output Stream.
- A stream is associated with file using openstatement.
- A stream is disassociated from file using closestatement.
- There following types of streams in C language:
- Text Stream
- Binary Stream
 
Text Stream
- It is a sequence of characters.
- A certain character translation may occur in text stream.
- It can only be used for text data
- It is less efficient than binary stream.
Binary Stream
- It is sequence of bytes.
- It has one-to-one relationship with external devices.
- No translation occurs in binary stream.
- It is more efficient than text stream.
- It can be used for different types of data.
File access methods
There are following methods to access a file:
- Sequential Access Method: It is used to access data is exact same sequence it is written.
- Random Access Method: It is used to access any data item directly without accessing the preceding data.
EOL & EOF
- EOL: It is character used to define end of line (At end of every line).
- EOF: It is character used to define end of file (At end of file).
Data file
- A collection of related records is called data file.
- It is a permanent storage of data.
- It can store any type of data.
- A type of data that stores data as printable and readable characters is called text file.
Pointer
- A pointer is a variable whose value is the address of another variable.
- It is initialized by:
- Memory address of a variable is given by:
- Value in memory cell is given by:
- Here is full code example:
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19 | #include <stdio.h>
int main () {
   int  var = 20;   /* actual variable declaration */
   int  *ip;        /* pointer variable declaration */
   ip = &var;  /* store address of var in pointer variable*/
   printf("Address of var variable: %x\n", &var  );
   /* address stored in pointer variable */
   printf("Address stored in ip variable: %x\n", ip );
   /* access the value using the pointer */
   printf("Value of *ip variable: %d\n", *ip );
   return 0;
}
 |  
 Output|  | Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
 |  
 
File Pointer
- It is a pointer that refers to a file in secondary storage
- It is variable of type FILEthat is defined instdio.h
- A program has to declare a file pointer to use a file.
- One file pointer can only refer to one data file.
- A file pointer variable is initialized by:
File Functions
fopen
fopen is used to open a file from further processing.
|  | file_pointer = fopen(file_name, mode);
 | 
- file_pointer is the name of file pointer declared in the program.
- file_name is the name of data file to be opened.
- mode is the mode in which file is to be opened.
| Mode | Function | 
| r | opened in read mode, data can't be modified, file must already exists | 
| w | opened in write mode, data can be written, existing data will be destroyed | 
| a | opened in append mode, data can't be read, data will be added at the end of existing data | 
| r+ | opened in read/write mode, data can be read or written, file must exist already | 
| w+ | opened in read/write mode, data can be read, existing data will be destroyed | 
| a+ | opened in append mode, data can be append at the end of file, existing data can also be read. | 
fclose
It is used to close connection between file and program.
- file_pointerpointer to the file to be closed.
fputc
Writes a character to a file
|  | fputc(char, file_pointer);
 | 
- charchar to be written.
- file_pointerof file where to written
fputs
Writes a string to a file.
|  | fputs(string, file_pointer)
 | 
* string to be written
* file_pointer of file where to written
fprintf
Writes a formatted string to file.
|  | fprintf(file_pointer, control_string, ...write_list)
 | 
- file_pointerof file where to written
- control_stringunformatted string containing formate specifiers.
- write_listlist of constants, variable & expressions to formate string.
fscanf