Skip to content

Latest commit

 

History

History
156 lines (124 loc) · 4.35 KB

ch23.md

File metadata and controls

156 lines (124 loc) · 4.35 KB

hero: 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:
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

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:
FILE *file

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.

fclose(file_pointer);
  • file_pointer pointer to the file to be closed.

fputc

Writes a character to a file

fputc(char, file_pointer);
  • char char to be written.
  • file_pointer of 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_pointer of file where to written
  • control_string unformatted string containing formate specifiers.
  • write_list list of constants, variable & expressions to formate string.

fscanf