File Handling - N K Upadhyay
-
Upload
dipayan-sarkar -
Category
Education
-
view
50 -
download
0
Transcript of File Handling - N K Upadhyay
![Page 1: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/1.jpg)
DATE:- 26/05/16
Presented By: Nitin Kumar Upadhyay K.V. Ramgarh Cantt
Region:- Ranchi
![Page 2: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/2.jpg)
What is File and what is the purpose of using a File?
A file can be defined as bunch of bytes stored on some storage media.
Files are used to store data permanently in C++.
![Page 3: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/3.jpg)
What is Stream and how I/O Operations takes place in a file?
A stream is a sequence of bytes or flow of data.
Stream is needed to perform input and output operations in a file.
![Page 4: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/4.jpg)
What are different types of File Streams?
There are two types of streams.
ofstream :- It is used to write data to a file.
Variable output stream File
ifstream :- It is used to read data from a file. File input stream variable
![Page 5: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/5.jpg)
To learn Merge and Search operations first we have to understand file opening modes used for above said purpose. Q. What is file opening mode Ans. File opening Mode defines type of operations which we can perform with the file. For Merge and Search operation following modes are used. ios::out:- It creates a new file for writing. ios::in :- It opens an existing file for reading. ios::app:- It is used to open an existing file to append more data inside the file.
![Page 6: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/6.jpg)
Functions/Operators to read/write data from a file.
Functions/Operators to read data from a file. <<, get( ), getline( ),read()
Functions/Operators to write data to a file.
>>,put( ),write( )
![Page 7: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/7.jpg)
Functions to read and write data from a file.
S.No.
Type of Data Function for Writing data
to a file
Function for Reading Data
from a file
01 Number << >>
02 char put( ),<< get( ),>>
03 Word << >>
04 String << >>
05 Line/Sentence << get( )getline( )
06 Record read( ) write( )
![Page 8: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/8.jpg)
To perform search operation in file.
It must be open in ios::in mode and the data must be read from file as per the type of data or requirement.For ex. (i)Suppose a question is given to count or perform any operation in file on the basis of alphabets like counting of vowels or blank spaces etc.
Then data must be read from file usingfstream.get(char_var);If stream object is f1 and char variable is ch then f1.get(ch);
![Page 9: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/9.jpg)
To perform search operation in file.
(ii) If question is given to count no of words or occurrence of a particular word then Then data must be read from file using file_stream>>string_var; If stream object is f1 and string variable is str then f1 >> str;
![Page 10: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/10.jpg)
To perform search operation in file.
(iii) if question is given to count no of lines present in a file. Then data must be read from file using file_stream.get(str,no_of_char,delim_char) If stream object is f1 and string variable is str then
f1.get(str,80,’\n’);or
f1.getline(str,80,’\n’);
![Page 11: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/11.jpg)
To perform search operation in file.
(iv) if question is given to count no of records Then data must be read from file using file_stream.read((char *)&o,sizeof(o)); If stream object is f1 and structure variable/class object is o then
f1.read((char *)&o,sizeof(o));
![Page 12: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/12.jpg)
To perform search operation in a file implemeted using structure.For example suppose stu is a structure with following definition and student.dat is a file which contains details of students as given structure.strcut stu{ int rollno; char name[30]; float per;}s;
![Page 13: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/13.jpg)
then to search record of a student having name sumit.void search( ){ fstream f1;char str[40]; f1.open(“student.dat”,ios::in|ios::binary); while(!f1.eof( )) { f1.read((char *)&s,sizeof(s)); if(strcmp(s.name,”sumit”)==0) cout<<s.rollno<<s.percentage } f1.close( );}
![Page 14: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/14.jpg)
To add one more record of a student.
void append( ){ fstream f1;student p; f1.open(“student.dat”,ios::app|ios::binary); while(!f1.eof( )) { cout<<“Input rollno,name and percentage”; cin>>s.rollno;gets(s.name);cin>>s.per; f1.write((char *)&s,sizeof(s)); } f1.close( );}
![Page 15: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/15.jpg)
#include<fstream.h> #include <conio.h> #include<stdio.h> void main( ) { char ch; char str[20]; fstream f1,f2,f3; f1.open("abc1.txt",ios::out); cout<<"input str"; gets(str);
![Page 16: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/16.jpg)
f1<<str; f1.close(); f2.open("abc2.txt",ios::out); cout<<"input str"; gets(str); f2<<str; f2.close(); f1.open("abc1.txt",ios::in); f2.open("abc2.txt",ios::in); f3.open("abc3.txt",ios::app);
![Page 17: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/17.jpg)
while(!f1.eof()) { f1.get(ch); f3.put(ch);} while(!f2.eof()) { f2.get(ch); f3.put(ch); } f1.close(); f2.close(); f3.close(); f3.open(“abc3.txt”,ios::in); while(!f3.eof()) { f3>>(str); cout<<str } f3.close(); }}
![Page 18: File Handling - N K Upadhyay](https://reader035.fdocument.pub/reader035/viewer/2022062822/5883ba4b1a28ab5c378b5601/html5/thumbnails/18.jpg)