Ch-14 (ICS II) - File Handling in C
Ch-14 (ICS II) - File Handling in C
Chapter 14
File Handling in C
Contents
• File, File Handling & Data file
• Types of Data File
• Data Access Methods
• Stream
• New-Line Marker & EOF
• Pointer
• Opening a File
• Closing a File
• Character Input / Output
• String Input / Output
• Formatted Input / Output
•
File, File Handling & Data
file
Q.1Define the terms file, file handling, and data file. Describe the advantages of file
handling.
File, File Handling & Data file
File
• File A collection of related data or records which is stored on disk with a specific
name
• Files are used to store different types of data on the secondary storage devices such
as hard disk or USB drive
File Handling
• A process to store data in files and to perform different operations of these files
• Computer processes data through program
• Input data and output results are stored temporarily in memory
• Erased from memory when computer is turned off
• In file handling, a program can read input data from a data file for processing
• Processed results can be stored into another data file
• Output data file created by one program can be used as input data file by another
program
File, File Handling & Data file
Data File
• A data file contains data or information
• In file handling, program reads data as input from data file
• Program processes data and writes output into a data file
• Data files are linked with a program during run-time to write and read data to and from data files
• More than one data file can be linked with a program
Advantages of File Handling
+ Saves time of user to enter data each time the program is executed
+ Input data can be checked before processing by program less chances of errors in output result
+ Execution of program becomes fast, because program has not to pause for getting input from user via
keyboard directly reads input from data file loaded in memory
+ Data stored in a data file (as input data) can easily be updated
+ Single program may read input data from many data files at the same time
+ Output returned by one program can be permanently saved on the disk for later use
+ Output of a program stored on a disk in a file can be used any time without running program
+ Output of a program stored on a disk can easily be printed on printer or sent by e-mails to other or it can
Types of Data File
Q.2 How many types of data files are used in C?
Text Files
• A type of file that stores data as readable and printable form
• User can easily understand data of text file
• Data of data file can also easily be printed on printer
• Source code of C program is an example of text file
• Text files can easily be created in any text editor programs such as Notepad in
Windows
• In C, text file (as data file) can be created and output of program can be stored
in it
• Data is accessed sequentially
• Data is stored in ASCII code
• One character or digit takes one byte in memory
• Number 3.5 will take 3 bytes (one byte for each digit including decimal point)
Binary Files
• A type of file that stores data in binary format
• Data of binary file is only understandable by computer
• In C, binary file can be created to store output of program
• User cannot understand the data of binary file
• Example object code of C program
• Binary file provides efficient way to access data
• Data can be accessed randomly and quickly
• In binary file
• Integer type data takes 2 bytes
• Float type data takes 4 bytes etc.
• For example: in binary format, integer value “29673” take only two bytes
• Binary format is used to organize large amount of data on disk
Data Access Methods
Q.3 Describe the data access methods.
Sequential Access Method
• Used to access required data from a file in a sequence
• Data is accessed in order, one after the other
• For example: to read record number 15 within a data file, program must have to read
records 1 to 14 in order to reach at record number 15
• Very slow access method
• Takes more time to access a required record
• Sequential access files Data files in which data is written and read
sequentially
• Formatted input/output files
• In C, text data files are sequential access data files
Direct Access Method
• Random access method is used to access required data in a file directly
• Very fast access method as compared to sequential access method
• Takes less time to access a required record
• Binary data files in C are direct access data files
Stream
Q.4 What is a stream? Illustrate the difference between text and binary
streams.
Stream
• A logical interface between data file or input/output device and program
• It is a general name given to a flow of data
• Data flows as a sequence of bytes
• Stream may be input stream or output stream
Input Stream
• A flow of data from input device to computer
• Examples:
• A program loaded in memory reads data from input device such as keyboard
• A program reads data from data file stored on disk
• Data flows from keyboard or disk to computer memory
Output Stream
• A flow of data from computer to output device
• Examples:
• A program loaded in memory sends information to output device such as monitor
• A program writes data into data file on disk
Types of Stream
Text Stream
• Text stream a sequence of characters
• In a text stream, certain character conversion may occur
• For example:
• When a sequence of characters flows from main memory to output device, a carriage return (‘\
r’) is converted into new line
• ‘\t’ is converted into horizontal tab and so on
• There may not be a one-to-one relationship between characters written and those
saved on an external device
Binary Stream
• Binary stream a sequence of bytes
• In a binary stream, there is one-to-one relationship between bytes and those saved
on external device
• Number of bytes written or read is the same as the number of bytes on external
New-Line Marker & EOF
Q.5 Distinguish between new-line and EOF markers.
New-Line Marker
• In a C-program, escape sequence ‘\n’ is used to insert a new-line on output
device and in a text data file
• ‘\n’ indicates end-of-line character or new-line marker
• In a text editor (such as TC editor or notepad), when Enter key is pressed, a
new line character ‘\n’ is automatically inserted at the end of a line
EOF
• EOF End-Of-File
• Special character
• Indicate the end of a text file
• Automatically inserted at the end of data file
• Sequential file When program is reading data, it reads data upto required
data item or end-of-file character is reached
• In C, EOF identifier is used to detect the end-of-file
Pointer
Q.6 What is pointer in C? Briefly explain its concept.
Q.7 What is file pointer? Why is it used in C language?
Pointer
• Variable, which points the memory address of another variable
• Pointer variable is used to store memory address of another variable
• Data type of pointer variable must be same as data type of variable whose
memory address is to be stored into pointer variable
• Declared in the similar way as ordinary variable except an asterisk (*) is placed
before pointer variable (or after data type)
• For example: int *x;
Address Operators (&)
• Address operator used to get memory address allocated to a specific variable
• Denoted by &
• Unary operator Reference operator
• & operator is used to get a memory address of a variable and assign to pointer
variable through assignment statement
Pointer
• Example
• If file is not in current directory (or you want to create a file in a particular
directory) then absolute path of file is given
• Escape sequence ‘\\’ is used in absolute path
File Opening Modes
• A data file can be opened in C to perform various operations
• Operations reading, writing and appending etc.
• Opening mode of a file indicates type of operation to be performed on data
file
• Different codes are used for opening mode
• “r” Reading mode, used if file already exists on disk
• “w” Writing mode, all data is deleted and file is opened as new file
• If data file does not exist, a new file is created
• “a” Append mode, new data is added at the end of current data of file
• “r+” Reading/writing mode, file must already exist on disk
• “w+” Reading/writing mode, If file already exists then its contents are overwritten
• “a+” Reading/appending mode, If file does not exist, a new file is created for both
reading and writing data
Verifying File Open Operation
• fopen() function returns NULL pointer if it fails to open data file
• Open operation may fail due to some error
• Possible errors disk full, disk error or file does not exist etc.
• Statements verify file open operation
Process the above data and write into data file “result.txt” in the following format.
PM Series
Computer Science
ICS Part-II
by
CM Aslam, Aqsa Aslam, Abdur Rehman &
Mudassir Saleem