Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

File Handling

Dr. Mayank Swarnkar

Department of Computer Science and Engineering


Indian Institute of Technology (BHU) Varanasi
mayank.cse@iitbhu.ac.in

Week-13

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-13 1 / 13


Introduction

Until now we used console for:


Taking user input
Showing output
Real life problems involves large volume of data
Unable to load everything in memory
Loss of data in case of power cut or memory error
Use of files to handle such problems

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-13 2 / 13


File Handling

We will study basic operations:


Naming a file
Opening a file
Reading data from a file
Writing data to a file
Closing a file

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-13 3 / 13


Defining and Opening a File

Data Structure: FILE


Uses file pointer
Declaration and opening:

FILE *fp;
fp = fopen("filename", "mode");

Figure: Defining and opening a file

filename: path of the file including its name

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-13 4 / 13


Modes of Operation

Mode Meaning
r read only
w write only
a append
r+ Existing file opened for reading and writing
w+ Same as w except both for reading and writing
r+ Same as a except both for reading and writing
Table: Modes of operation

Any number of files can be opened at a time

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-13 5 / 13


Closing a File

File must be closed at the end of program


This is important because:
Ensures buffer flush
Break all memory links with the file
Memory leakage
Unwanted behavior of program
Format: fclose(file-pointer);
Need to close all file pointers used in program

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-13 6 / 13


IO Operations in File

Function Example Meaning


getc c = getc(fp); Read a character from a file
putc putc(c, fp); Write a character to a file
getw i = getw(fp) Read an integer from a file
putw putw(i, fp) Write an integer to a file
fscanf fscanf(fp, “control string”, arg1 , arg2 ... argn ) same as scanf but for a file
fprintf fprintf(fp, “control string”, arg1 , arg2 ... argn ); same as printf but for a file

Table: Modes of operation

End of File
Similar to End of String
Keyword: EOF
Usually used to check anything till end of file

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-13 7 / 13


Exercise

Read a given file and find the number of vowels and consonants in
that file. Save this information in another file in the following format:
Vowels: Count of vowels
Consonants: Count of consonants

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-13 8 / 13


Error Handling During I/O Operations

Some possible situations:


Reading beyond EOF
Device memory overflow
File not available or not found
File busy or used by other program
Writing in a write protected file
Such unchecked error results in:
Premature termination of program
Abnormal behavior of program resulting wrong outputs
Handling errors:
feof
ferror

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-13 9 / 13


Error Handling During I/O Operations

feof: test for end of file condition


if(feof(fp))
printf("End of data \n");

Figure: Example for feof

ferror: returns 0 for normal else integers for errors

if(ferror(fp) != 0)
printf("Error occured \n");

Figure: Example for ferror

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-13 10 / 13


Random Access to a File

This can be done using following three functions:


ftell:
n = ftell(fp);
where ‘n’ is the relative offset in bytes
rewind:
rewind(fp);
resets the position of file pointer to start
fseek:
fseek(fp, offset, position)
offset is the number of bytes to be moved with respect to position
position: 0 for start, 1 for current and 2 for end of file

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-13 11 / 13


Exercise

Write a program which take following three input from user:


w1 : A word to be replaced in a file
w2 : A word which replace w1 in a file
n: Number of times replacement to be done
Any negative number means replace all
If n > Existing words then report the number of replacements made
Do not report otherwise

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-13 12 / 13


End of Lecture

Dr. Mayank Swarnkar (IIT(BHU) Varanasi) Subject: C Programming Week-13 13 / 13

You might also like