Skip to content
23 - File Handling

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 open statement.
  • A stream is disassociated from file using close statement.
  • 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:
    1
    int *pointer_var;
    
  • Memory address of a variable is given by:
    1
    pointer_var = &var_name
    
  • Value in memory cell is given by:
    1
    data = *pointer_var
    
  • 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
    1
    2
    3
    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 FILE that is defined in stdio.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:
    1
    FILE *file
    

File Functions

fopen

fopen is used to open a file from further processing.

1
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.

1
fclose(file_pointer);

  • file_pointer pointer to the file to be closed.

fputc

Writes a character to a file

1
fputc(char, file_pointer);

  • char char to be written.
  • file_pointer of file where to written

fputs

Writes a string to a file.

1
fputs(string, file_pointer)
* string to be written * file_pointer of file where to written

fprintf

Writes a formatted string to file.

1
fprintf(file_pointer, control_string, ...write_list)

  • file_pointer of file where to written
  • control_string unformatted string containing formate specifiers.
  • write_list list of constants, variable & expressions to formate string.

fscanf

Comments