SlideShare a Scribd company logo
DATE:- 26/05/16
Presented By: Nitin Kumar Upadhyay
K.V. Ramgarh Cantt
Region:- Ranchi
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++.
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.
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
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.
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( )
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( )
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 using
fstream.get(char_var);
If stream object is f1 and char variable is ch then
f1.get(ch);
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;
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’);
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));
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;
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( );
}
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( );
}
 #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);
 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);
 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();
File Handling - N K Upadhyay

More Related Content

PDF
File Types in Data Structure
PDF
Python file handling
PPTX
UNIT 10. Files and file handling in C
PPTX
Chapter 08 data file handling
PPTX
Handling computer files
PDF
FILES IN C
PPTX
Data file handling in python introduction,opening & closing files
PPTX
basics of file handling
File Types in Data Structure
Python file handling
UNIT 10. Files and file handling in C
Chapter 08 data file handling
Handling computer files
FILES IN C
Data file handling in python introduction,opening & closing files
basics of file handling

What's hot (20)

ODP
Flat Filer Presentation
PPTX
5 Structure & File.pptx
PPTX
Pf cs102 programming-8 [file handling] (1)
DOCX
Switching & Multiplexing
PPTX
Streams and Files
PPTX
Indexing structure for files
PPT
Mesics lecture files in 'c'
PPTX
File handling in Python
PDF
File handling
PPTX
Files and file objects (in Python)
PDF
Files in C
PPTX
C programming disk file reading and writing
PPT
File organization 1
PPT
File handling
PDF
Python programming : Files
PPTX
Chapter 10.1
PPT
17 files and streams
PPT
PPTX
File handling in vb.net
Flat Filer Presentation
5 Structure & File.pptx
Pf cs102 programming-8 [file handling] (1)
Switching & Multiplexing
Streams and Files
Indexing structure for files
Mesics lecture files in 'c'
File handling in Python
File handling
Files and file objects (in Python)
Files in C
C programming disk file reading and writing
File organization 1
File handling
Python programming : Files
Chapter 10.1
17 files and streams
File handling in vb.net
Ad

Viewers also liked (9)

PDF
eko2014
PDF
UNFE Final Report_v3
DOCX
OPWDD resume Garruto
PDF
Adele Marra
PDF
Manual del usuario
PPT
Luca Soranzo
PDF
Learning analytics support for just-in-time teaching
PDF
003 cuarto reyesamezquita_helendaniela (1)
PDF
Dossier Beat the Odds
eko2014
UNFE Final Report_v3
OPWDD resume Garruto
Adele Marra
Manual del usuario
Luca Soranzo
Learning analytics support for just-in-time teaching
003 cuarto reyesamezquita_helendaniela (1)
Dossier Beat the Odds
Ad

Similar to File Handling - N K Upadhyay (20)

PPTX
Filesin c++
PPT
data file handling
PPT
7 Data File Handling
PDF
Files and streams
PDF
Filehadnling
PPT
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
PDF
8. file handling
PDF
File handling
PPTX
Working with files in c++. file handling
PPSX
Files in c++
PDF
Filesinc 130512002619-phpapp01
PPT
Data file handling
PPTX
Cs1123 10 file operations
PDF
Data file handling
PPTX
pointer, structure ,union and intro to file handling
PDF
Basic file operations CBSE class xii ln 7
PPTX
working with files
PDF
Filepointers1 1215104829397318-9
PDF
DOC-20241121-WA0004bwushshusjssjuwh..pdf
PPTX
Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handling
Filesin c++
data file handling
7 Data File Handling
Files and streams
Filehadnling
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
8. file handling
File handling
Working with files in c++. file handling
Files in c++
Filesinc 130512002619-phpapp01
Data file handling
Cs1123 10 file operations
Data file handling
pointer, structure ,union and intro to file handling
Basic file operations CBSE class xii ln 7
working with files
Filepointers1 1215104829397318-9
DOC-20241121-WA0004bwushshusjssjuwh..pdf
Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handling

More from Dipayan Sarkar (18)

PPT
Relationships
PPTX
ideal learning space
PPTX
introduction to aep arc
PPTX
why focus on adolescents unique needs
PPT
aep in india milestones, learning, way forward
PDF
Softskills - S Fernandez
PDF
Project Based Learning- Ashish K Chaurdia
PDF
Linux and the Open Source- D Sarkar
PPTX
Sorting Techniques - Govardhan Satish
PPTX
Application of Stack - Yadraj Meena
PPT
Information Technology Act 2000 - Santosh K Pathak
PPTX
Universal Gates - Aneesa N Ali
PPT
Selection Sort - Vipin Ramola
PPT
Deletion of a Record from a File - K Karun
PPTX
Java Databse Connectvity- Alex Jose
PPTX
Computer Viruses- B S Kalyan Chakravarthy
PPT
Cloud Computing- Sreehari S
PPTX
SQL JOINS- Reena P V
Relationships
ideal learning space
introduction to aep arc
why focus on adolescents unique needs
aep in india milestones, learning, way forward
Softskills - S Fernandez
Project Based Learning- Ashish K Chaurdia
Linux and the Open Source- D Sarkar
Sorting Techniques - Govardhan Satish
Application of Stack - Yadraj Meena
Information Technology Act 2000 - Santosh K Pathak
Universal Gates - Aneesa N Ali
Selection Sort - Vipin Ramola
Deletion of a Record from a File - K Karun
Java Databse Connectvity- Alex Jose
Computer Viruses- B S Kalyan Chakravarthy
Cloud Computing- Sreehari S
SQL JOINS- Reena P V

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Cell Structure & Organelles in detailed.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
Classroom Observation Tools for Teachers
PPTX
master seminar digital applications in india
PPTX
Lesson notes of climatology university.
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Complications of Minimal Access Surgery at WLH
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
O7-L3 Supply Chain Operations - ICLT Program
Cell Structure & Organelles in detailed.
Final Presentation General Medicine 03-08-2024.pptx
Pharma ospi slides which help in ospi learning
Classroom Observation Tools for Teachers
master seminar digital applications in india
Lesson notes of climatology university.
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
102 student loan defaulters named and shamed – Is someone you know on the list?
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Anesthesia in Laparoscopic Surgery in India
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Supply Chain Operations Speaking Notes -ICLT Program
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Complications of Minimal Access Surgery at WLH
O5-L3 Freight Transport Ops (International) V1.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

File Handling - N K Upadhyay

  • 1. DATE:- 26/05/16 Presented By: Nitin Kumar Upadhyay K.V. Ramgarh Cantt Region:- Ranchi
  • 2. 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++.
  • 3. 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.
  • 4. 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
  • 5. 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.
  • 6. 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( )
  • 7. 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( )
  • 8. 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 using fstream.get(char_var); If stream object is f1 and char variable is ch then f1.get(ch);
  • 9. 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;
  • 10. 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’);
  • 11. 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));
  • 12. 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;
  • 13. 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( ); }
  • 14. 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( ); }
  • 15.  #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);
  • 16.  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);
  • 17.  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();