File handling in ‘C’ C

  • File basics.
  • ‘C’ language support for files.

Right at the outset, let us see, what exactly is meant by files and how ‘C’ language facilitates file handling. We will also discuss about buffer and its use in relation to a file.

File Basics
Files are divided basically into two types:

  • Stream- oriented (standard or high-level)
  • System- oriented (or low level)

Stream oriented data files are easier to work with than system-oriented data-files and thus more commonly used.

Numbers of library functions are used to operate on files which are broadly classified as

  • High level file I/O functions
  • Low level file I/O functions

High level file I/O functions do their own buffer management whereas in low level file I/O functions buffer management has to be done by the programmer.
Now, what is a buffer? A buffer is a block of memory where the data to be stored in file is placed temporarily.
E.g.
If an user enters characters to be written to a file, through the keyboard, these characters are not directly written to the file but are temporarily stored in buffer. When buffer is full, disk is accessed and contents transferred to the file on disk. Or when end of file is reached buffer contents are transferred to disk file.

What will happen if data is directly send to disk file instead of using buffer? Since accessing disk involves mechanical movements, if disk has to be accessed for each character, it will take a lot of time. Thus buffer allows information to be read from and written to data files more rapidly.

High level file I/O functions are further categorized into text and binary, which are in turn categorized into formatted and unformatted.

C-language support for files
‘C’ language does not give any facility for input and output. Each operating system has its own facility for inputting and outputting data from and to files and devices. The system programmer writes small programs that link the ‘C’ compiler for particular operating systems I/O facility.

There are several standard I/O functions available in libraries. The following figure gives a clear picture.

C-language support for files

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C Topics