hero: 23 - File Handling
- 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
open
statement. - A stream is disassociated from file using
close
statement. - There following types of streams in C language:
- Text Stream
- Binary 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.
- 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.
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: 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).
- 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.
- A pointer is a variable whose value is the address of another variable.
- It is initialized by:
int *pointer_var;
- Memory address of a variable is given by:
pointer_var = &var_name
- Value in memory cell is given by:
data = *pointer_var
- Here is full code example:
#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
- It is a pointer that refers to a file in secondary storage
- It is variable of type
FILE
that 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 *file
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. |
It is used to close connection between file and program.
fclose(file_pointer);
file_pointer
pointer to the file to be closed.
Writes a character to a file
fputc(char, file_pointer);
char
char to be written.file_pointer
of file where to written
Writes a string to a file.
fputs(string, file_pointer)
string
to be writtenfile_pointer
of file where to written
Writes a formatted string to file.
fprintf(file_pointer, control_string, ...write_list)
file_pointer
of file where to writtencontrol_string
unformatted string containing formate specifiers.write_list
list of constants, variable & expressions to formate string.