SlideShare a Scribd company logo
1 of 17
.
FILES IN C++
TITLES:
• Use of header file fstream.h
• Types of stream objects
• Open() and close() functions
• File mode constants
• Reading and writing characters from to disk
• Reading and writing objects from to disk
FILE STREAM:
• A file stream act as an interface between program and the files
• The stream that supplies data to the program is known as input stream
• The stream that receives data from the program is known as output stream
• Input stream
• read data data
I/P
• Output stream
• Write data data
O/P
ProgramDisk file
CLASSES FOR FILE STREAM OPERATIONS:
• Io stream file
• F stream file
ios
Stream buf O streamI stream
Io stream
If stream F stream Of stream File buf
F stream base
USING F STREAM.H:
• A stream is a sequence of bytes.
• It is general name given to a flow of data.
• Different streams are used to represent different kinds of data flow.
• ifstream class represents input disk files.
• Ofstream class represents output disk files.
• Fstream for both input and output
DIFFERENT CLASSES AND ITS FUNCTIONS:
class functions
File buf It sets the file buffers to read and write.
Member function : open(),close()
Fstream base This is the base class for fstream,ifstream
and ofstream classes.
Member function : All input and output
function open(),close()
ifstream It provide input operation for file.
Member function : get(),getline (),read
(),seekg (),tellg ()
ofstream It provide output operation for file
Member function : put (),write (),seekp (),tellp
()
fstream It is an input and output stream.
OPENING AND CLOSING FILES:
• Opening of files can be achieved in two ways:
1. using the constructor function of the stream class
syntax : stream stream object (“name of file name”);
2. using the function open()
syntax : stream stream object;
stream object . Open (“name of file name”);
TYPES OF FILES:
• Files are two types:
1. ASCII files or text files:
those files created by storing characters.
2.Binary file:
those files created by storing a block of memory.
THE CONCEPT OF FILE MODES:
• It describes how a file is to be used
1. To read from it.
2. To write to it.
3. To append it.
4. To read and write and so on..
syntax : stream object . Open (“file name”, file_mode);
TYPES OF FILES:
s.no File modes meaning Stream type
1. ios::in It opens file for
reading
Ifstream
2. ios::out It opens file for
writing
ofstream
3. ios::app It causes all output to
that file to be
appended to the end
ofstream
4. ios::ate It seeks to end-of-file
upon opening of the
file
ofstream
5. ios::trunc Delete contents of
the file if it exists
ofstream
6. ios::nocreate It causes the open ()
functions to fail if the
file does not already
exit.it will not create
a new file with that
ofstream
READING AND WRITING CHARACTERS FROM TO
DISK:
• The function put () and get () are used for manipulating a file
character by character.
• These function are members of ostream and istream
respectively
put() is used for output to the file.
get() is used for input from file.
#include <fstream.h>
Void main()
{
char ch;
ifstream infile (“out.txt”);
while (infile)
{
infile.get (ch);
cout << ch;
}
infile . Close();l
}
#include <fstream.h>
void main()
{
ofstream outfile (“out.txt”);
char str[]=“This is a text file”;
int i=0;
while (str[i])
outfile.put (str [i++]);
outfile.close ();
}
EXAMPLE PROGRAMS:
To create a file using put() To read a file using get()
READING AND WRITING CLASS OBJECTS FROM
TO DISK:
• The functions write() and read() are usually used to transfer a
block of data from and to the file.
• Write () is used for output to the file.
• Read () is used for input from file.
• To get the contents from the file
• It takes two arguments
i.e., a pointer to the block and
the size of the block
Eg : std file. Read
((char*&s,sizeof(student));
• It takes two arguments.
i.e., a pointer to the block and
the size of the block.
Eg : std file.write
((char*)&s,sizeof(student));
.
To write to the file To read the file
PROGRAM TO CREATE A STUDENT FILE:
#include<fstream.h>
Class student
{
private :
int regno,mark;
char name [20];
public :
void getdata ();
};
void student :: getdata ()
{
cout <<“n enter reg.number :”;
cin >> reg no;
cout <<“n enter name of students”;
gets (name);
cout <<“nenter marks:”;
cin >> marksw;
}
.
Void student :: display ()
{
cout << “n register number :”<<reg no;
cout << “n na,me of student :”<<name;
cout << “n marks :”<<marks;
}
Void main()
{
student ob;
fstream std file;
Std file.open (‘stud.dat”,ios :: in);
std file.read ((char*)&ob,sizeof(student));
while (std file)
{
ob.display();
std file.read((char*)&ob,sizeof(student));
}
std file.close();
}
OUTPUT:
• Open for reading only
• I/P POINTER
• Open for writing only
• O/P POINTER
• Open for append mode
H A I
H A I

More Related Content

What's hot

What's hot (20)

Java IO
Java IOJava IO
Java IO
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
The string class
The string classThe string class
The string class
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
File in c
File in cFile in c
File in c
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
08. handling file streams
08. handling file streams08. handling file streams
08. handling file streams
 
Exception handling
Exception handlingException handling
Exception handling
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 

Similar to Files in c++ (20)

Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
 
File handling
File handlingFile handling
File handling
 
Filehadnling
FilehadnlingFilehadnling
Filehadnling
 
Data file handling
Data file handlingData file handling
Data file handling
 
File management in C++
File management in C++File management in C++
File management in C++
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSFILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
 
File Handling
File HandlingFile Handling
File Handling
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
 
Streams and Files
Streams and FilesStreams and Files
Streams and Files
 
Data file handling
Data file handlingData file handling
Data file handling
 
Input File dalam C++
Input File dalam C++Input File dalam C++
Input File dalam C++
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
Python file handling
Python file handlingPython file handling
Python file handling
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
637225560972186380.pdf
637225560972186380.pdf637225560972186380.pdf
637225560972186380.pdf
 

Recently uploaded

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Recently uploaded (20)

Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

Files in c++

  • 2. TITLES: • Use of header file fstream.h • Types of stream objects • Open() and close() functions • File mode constants • Reading and writing characters from to disk • Reading and writing objects from to disk
  • 3. FILE STREAM: • A file stream act as an interface between program and the files • The stream that supplies data to the program is known as input stream • The stream that receives data from the program is known as output stream • Input stream • read data data I/P • Output stream • Write data data O/P ProgramDisk file
  • 4. CLASSES FOR FILE STREAM OPERATIONS: • Io stream file • F stream file ios Stream buf O streamI stream Io stream If stream F stream Of stream File buf F stream base
  • 5. USING F STREAM.H: • A stream is a sequence of bytes. • It is general name given to a flow of data. • Different streams are used to represent different kinds of data flow. • ifstream class represents input disk files. • Ofstream class represents output disk files. • Fstream for both input and output
  • 6. DIFFERENT CLASSES AND ITS FUNCTIONS: class functions File buf It sets the file buffers to read and write. Member function : open(),close() Fstream base This is the base class for fstream,ifstream and ofstream classes. Member function : All input and output function open(),close() ifstream It provide input operation for file. Member function : get(),getline (),read (),seekg (),tellg () ofstream It provide output operation for file Member function : put (),write (),seekp (),tellp () fstream It is an input and output stream.
  • 7. OPENING AND CLOSING FILES: • Opening of files can be achieved in two ways: 1. using the constructor function of the stream class syntax : stream stream object (“name of file name”); 2. using the function open() syntax : stream stream object; stream object . Open (“name of file name”);
  • 8. TYPES OF FILES: • Files are two types: 1. ASCII files or text files: those files created by storing characters. 2.Binary file: those files created by storing a block of memory.
  • 9. THE CONCEPT OF FILE MODES: • It describes how a file is to be used 1. To read from it. 2. To write to it. 3. To append it. 4. To read and write and so on.. syntax : stream object . Open (“file name”, file_mode);
  • 10. TYPES OF FILES: s.no File modes meaning Stream type 1. ios::in It opens file for reading Ifstream 2. ios::out It opens file for writing ofstream 3. ios::app It causes all output to that file to be appended to the end ofstream 4. ios::ate It seeks to end-of-file upon opening of the file ofstream 5. ios::trunc Delete contents of the file if it exists ofstream 6. ios::nocreate It causes the open () functions to fail if the file does not already exit.it will not create a new file with that ofstream
  • 11. READING AND WRITING CHARACTERS FROM TO DISK: • The function put () and get () are used for manipulating a file character by character. • These function are members of ostream and istream respectively put() is used for output to the file. get() is used for input from file.
  • 12. #include <fstream.h> Void main() { char ch; ifstream infile (“out.txt”); while (infile) { infile.get (ch); cout << ch; } infile . Close();l } #include <fstream.h> void main() { ofstream outfile (“out.txt”); char str[]=“This is a text file”; int i=0; while (str[i]) outfile.put (str [i++]); outfile.close (); } EXAMPLE PROGRAMS: To create a file using put() To read a file using get()
  • 13. READING AND WRITING CLASS OBJECTS FROM TO DISK: • The functions write() and read() are usually used to transfer a block of data from and to the file. • Write () is used for output to the file. • Read () is used for input from file.
  • 14. • To get the contents from the file • It takes two arguments i.e., a pointer to the block and the size of the block Eg : std file. Read ((char*&s,sizeof(student)); • It takes two arguments. i.e., a pointer to the block and the size of the block. Eg : std file.write ((char*)&s,sizeof(student)); . To write to the file To read the file
  • 15. PROGRAM TO CREATE A STUDENT FILE: #include<fstream.h> Class student { private : int regno,mark; char name [20]; public : void getdata (); }; void student :: getdata () { cout <<“n enter reg.number :”; cin >> reg no; cout <<“n enter name of students”; gets (name); cout <<“nenter marks:”; cin >> marksw; }
  • 16. . Void student :: display () { cout << “n register number :”<<reg no; cout << “n na,me of student :”<<name; cout << “n marks :”<<marks; } Void main() { student ob; fstream std file; Std file.open (‘stud.dat”,ios :: in); std file.read ((char*)&ob,sizeof(student)); while (std file) { ob.display(); std file.read((char*)&ob,sizeof(student)); } std file.close(); }
  • 17. OUTPUT: • Open for reading only • I/P POINTER • Open for writing only • O/P POINTER • Open for append mode H A I H A I