File Handling - PowerPoint Presentation, Programming & Data Structures Notes | EduRev

: File Handling - PowerPoint Presentation, Programming & Data Structures Notes | EduRev

 Page 1


1
Dept. of CSE, IIT KGP
File Handling
File Handling
 
 
CS10001:
CS10001:
 
 
Programming & Data Structures
Programming & Data Structures
Pallab Dasgupta
Pallab Dasgupta
Professor, Dept. of Computer 
Professor, Dept. of Computer 
Sc. & Engg.,
Sc. & Engg.,
Indian Institute of 
Indian Institute of 
Technology Kharagpur
Technology Kharagpur
Page 2


1
Dept. of CSE, IIT KGP
File Handling
File Handling
 
 
CS10001:
CS10001:
 
 
Programming & Data Structures
Programming & Data Structures
Pallab Dasgupta
Pallab Dasgupta
Professor, Dept. of Computer 
Professor, Dept. of Computer 
Sc. & Engg.,
Sc. & Engg.,
Indian Institute of 
Indian Institute of 
Technology Kharagpur
Technology Kharagpur
2
Dept. of CSE, IIT KGP
What is a file?
What is a file?
•
A named collection of data, stored in secondary 
A named collection of data, stored in secondary 
storage (typically).
storage (typically).
•
Typical operations on files:
Typical operations on files:
–
Open Open
–
Read Read
–
Write Write
–
Close Close
•
How is a file stored?
How is a file stored?
–
Stored as sequence of bytes, logically contiguous (may not be Stored as sequence of bytes, logically contiguous (may not be 
physically contiguous on disk). physically contiguous on disk).
Page 3


1
Dept. of CSE, IIT KGP
File Handling
File Handling
 
 
CS10001:
CS10001:
 
 
Programming & Data Structures
Programming & Data Structures
Pallab Dasgupta
Pallab Dasgupta
Professor, Dept. of Computer 
Professor, Dept. of Computer 
Sc. & Engg.,
Sc. & Engg.,
Indian Institute of 
Indian Institute of 
Technology Kharagpur
Technology Kharagpur
2
Dept. of CSE, IIT KGP
What is a file?
What is a file?
•
A named collection of data, stored in secondary 
A named collection of data, stored in secondary 
storage (typically).
storage (typically).
•
Typical operations on files:
Typical operations on files:
–
Open Open
–
Read Read
–
Write Write
–
Close Close
•
How is a file stored?
How is a file stored?
–
Stored as sequence of bytes, logically contiguous (may not be Stored as sequence of bytes, logically contiguous (may not be 
physically contiguous on disk). physically contiguous on disk).
3
Dept. of CSE, IIT KGP
–
The last byte of a file contains the end-of-file character ( The last byte of a file contains the end-of-file character (EOF EOF), ), 
with ASCII code with ASCII code 1A (hex). 1A (hex).
–
While reading a text file, the EOF character can be checked to While reading a text file, the EOF character can be checked to 
know the end. know the end.
•
Two kinds of files:
Two kinds of files:
–
Text :: contains ASCII codes only Text :: contains ASCII codes only
–
Binary :: can contain non-ASCII characters Binary :: can contain non-ASCII characters
•
Image, audio, video, executable, etc. Image, audio, video, executable, etc.
•
To check the end of file here, the To check the end of file here, the file size file size value (also stored on  value (also stored on 
disk) needs to be checked. disk) needs to be checked.
Page 4


1
Dept. of CSE, IIT KGP
File Handling
File Handling
 
 
CS10001:
CS10001:
 
 
Programming & Data Structures
Programming & Data Structures
Pallab Dasgupta
Pallab Dasgupta
Professor, Dept. of Computer 
Professor, Dept. of Computer 
Sc. & Engg.,
Sc. & Engg.,
Indian Institute of 
Indian Institute of 
Technology Kharagpur
Technology Kharagpur
2
Dept. of CSE, IIT KGP
What is a file?
What is a file?
•
A named collection of data, stored in secondary 
A named collection of data, stored in secondary 
storage (typically).
storage (typically).
•
Typical operations on files:
Typical operations on files:
–
Open Open
–
Read Read
–
Write Write
–
Close Close
•
How is a file stored?
How is a file stored?
–
Stored as sequence of bytes, logically contiguous (may not be Stored as sequence of bytes, logically contiguous (may not be 
physically contiguous on disk). physically contiguous on disk).
3
Dept. of CSE, IIT KGP
–
The last byte of a file contains the end-of-file character ( The last byte of a file contains the end-of-file character (EOF EOF), ), 
with ASCII code with ASCII code 1A (hex). 1A (hex).
–
While reading a text file, the EOF character can be checked to While reading a text file, the EOF character can be checked to 
know the end. know the end.
•
Two kinds of files:
Two kinds of files:
–
Text :: contains ASCII codes only Text :: contains ASCII codes only
–
Binary :: can contain non-ASCII characters Binary :: can contain non-ASCII characters
•
Image, audio, video, executable, etc. Image, audio, video, executable, etc.
•
To check the end of file here, the To check the end of file here, the file size file size value (also stored on  value (also stored on 
disk) needs to be checked. disk) needs to be checked.
4
Dept. of CSE, IIT KGP
File handling in C
File handling in C
•
In C we use In C we use FILE * FILE *  to represent a pointer to a file.  to represent a pointer to a file.  
•
fopen fopen  is used to open a file.  It returns the special value is used to open a file.  It returns the special value NULL NULL to  to 
indicate that it is unable to open the file. indicate that it is unable to open the file.
FILE *fptr;
char filename[]= "file2.dat";
fptr = fopen (filename,"w");
if (fptr == NULL) {
  printf (“ERROR IN FILE CREATION”);  
     /* DO SOMETHING */
}
Page 5


1
Dept. of CSE, IIT KGP
File Handling
File Handling
 
 
CS10001:
CS10001:
 
 
Programming & Data Structures
Programming & Data Structures
Pallab Dasgupta
Pallab Dasgupta
Professor, Dept. of Computer 
Professor, Dept. of Computer 
Sc. & Engg.,
Sc. & Engg.,
Indian Institute of 
Indian Institute of 
Technology Kharagpur
Technology Kharagpur
2
Dept. of CSE, IIT KGP
What is a file?
What is a file?
•
A named collection of data, stored in secondary 
A named collection of data, stored in secondary 
storage (typically).
storage (typically).
•
Typical operations on files:
Typical operations on files:
–
Open Open
–
Read Read
–
Write Write
–
Close Close
•
How is a file stored?
How is a file stored?
–
Stored as sequence of bytes, logically contiguous (may not be Stored as sequence of bytes, logically contiguous (may not be 
physically contiguous on disk). physically contiguous on disk).
3
Dept. of CSE, IIT KGP
–
The last byte of a file contains the end-of-file character ( The last byte of a file contains the end-of-file character (EOF EOF), ), 
with ASCII code with ASCII code 1A (hex). 1A (hex).
–
While reading a text file, the EOF character can be checked to While reading a text file, the EOF character can be checked to 
know the end. know the end.
•
Two kinds of files:
Two kinds of files:
–
Text :: contains ASCII codes only Text :: contains ASCII codes only
–
Binary :: can contain non-ASCII characters Binary :: can contain non-ASCII characters
•
Image, audio, video, executable, etc. Image, audio, video, executable, etc.
•
To check the end of file here, the To check the end of file here, the file size file size value (also stored on  value (also stored on 
disk) needs to be checked. disk) needs to be checked.
4
Dept. of CSE, IIT KGP
File handling in C
File handling in C
•
In C we use In C we use FILE * FILE *  to represent a pointer to a file.  to represent a pointer to a file.  
•
fopen fopen  is used to open a file.  It returns the special value is used to open a file.  It returns the special value NULL NULL to  to 
indicate that it is unable to open the file. indicate that it is unable to open the file.
FILE *fptr;
char filename[]= "file2.dat";
fptr = fopen (filename,"w");
if (fptr == NULL) {
  printf (“ERROR IN FILE CREATION”);  
     /* DO SOMETHING */
}
5
Dept. of CSE, IIT KGP
Modes for opening files
Modes for opening files
•
The second argument of 
The second argument of 
fopen
fopen
 is the 
 is the 
mode
mode
 
 
in which 
in which 
we open the file.  There are three modes.
we open the file.  There are three modes.
  
"r" "r" opens a file for reading. opens a file for reading.
"w" "w" creates a file for writing, and writes over all previous  creates a file for writing, and writes over all previous 
contents (deletes the file so be careful!). contents (deletes the file so be careful!).
"a" "a" opens a file for appending – writing on the end of the file. opens a file for appending – writing on the end of the file.
Read More
Offer running on EduRev: Apply code STAYHOME200 to get INR 200 off on our premium plan EduRev Infinity!