SlideShare a Scribd company logo
1 of 14
Download to read offline
Files in C++
Files and Stream classes
Files the collection of records or programs of same nature.
In C++, the concept of stream or stream classes to implement the I/O operations
with the console and disk files.
The I/O devices such as terminals, keyboards, disk etc. supplies an interface to
the programmer for the smooth working with the system. This interface is known
as stream.
A stream is a sequence of bytes. It acts either as a source from which the input
data can be obtained or a destination to which the output data can be sent.
The source stream that provides data to the program is called the input stream
and the destination stream that receives output from the program is known as
output stream.
cin and cout are used earlier in programs to perform this input and output
operations
Files and Stream classes
ios
ostream
streambuf
istream
iostream
istream_withassign Iostream_withassign ostream_withassign
Input
Pointer
output
Stream classes for console operations
Class Name Contents
ios Contains all basic facilities of input and output classes
istream get(), getline(), read() and operator such as >>
ostream put() and write(), insertion operator <<
iostream All input and output functions(ios/istream/ostream)
streambuf It acts as a base for filebuf class used in files
Unformatted I/O operations
cin>>var1>>var2>>……>>varn;
cout<<var1<<var2<<……<<varn;
Eg:
put() and get()
char c;
cin.get(c);
char c;
cin.get(c);
cout.put(c);
Unformatted I/O operations
getline() and write()
The getline() reads a whole line of text end with newline character(or when we
press return(enter) key)
cin.getline(line, size);
Eg;
char name[20];
cin.getline(name, 20);
cout.write(line, size);
The write() displays/prints a whole line of text
Classes for file stream operations
ios
ostream
streambuf
istream
iostream
ifstream fstream ofstream
Input
Pointer
output
filebuf
fstream base
iostream.h file
fstream.h file
Details of file stream classes
Class Contents
filebuf Set the file buffers to read and write. It contains close() and open() as
members
fstreambase Provides operations common to the file streams, serves the base for
ifsteram, ofstream, fstream classes. It contains open() and close() functions
ifstream Provide input operations. Contains open() as default input mode. Inherits
the functions get(), getline(), read(), seekg() and tellg()
ofstream Provide output operations. Contains close() as default output mode. Inherits
the functions put(), write(), seekp() and tellp()
fstream Provides all input and output operations. It performs all functions of
ifstream and ofstream. Inherits all istream and ostream classes through
iostream
Opening and closing a file
• Suitable name for the file
• Data type structure
• Purpose
• Opening method
• Input.data
• Test.doc
• Student
• output
Opening and closing a file
• Opening method
1. Using the constructor function of the class
Here constructors are used in files to initialize the file stream object.
1. Choose appropriate filestream object
2. Initialize the object using suitable file name
ofstream outfile(“results”); // this creates outfile as an ofstream object that manages the
output stream
This statement also opens the file results and attaches it to the output stream outfile
Ifstream infile(“input.dat”);
outfile<<“Total”;
outfile<<sum;
infile>> rollno;
outfile.close(); //to close the outfile
infile.close();
Opening and closing a file
• Opening method
1. Using the open() function for opening the file
The function open() can be used to open multiple files that use the same stream object.
file_stream_class stream_object;
Stream_object.open(“filename”);
ofstream outfile;
outfile.open(“Data1”);
……………………..
outfile.close();
outfile.open(“Data2”);
……………………………
outfile.close();
File Opening modes
• Opening modes
• It specifies the purpose of opening the file
stream_object.open(“file_name”, mode);
Mode specifies the purpose for which the file is opened.
Default mode for ifstream is ios::in and for ofstream is ios::out
Parameter Meaning
ios::app Append to end-of-file
ios::ate Go to the end-of-file on opening
ios::binary Binary file
ios::in Open file for reading only
ios::nocreate Open fails if the file does not exists
ios::noreplace Open fails if the file exists
ios::out Open files for writing only
ios::trunc Delete contents of the file if it exists
File pointers and their manipulations
Each file has two associated pointers known as file pointers.
One is the input pointer (get pointer) and other one is output pointer
(put pointer)
We are using it for reading and writing the files
seekg() Moves get pointer to a specified position
seekp() Moves the put pointer to a specified position
tellg() Gives the current position of the get pointer
tellp() Gives the current position of the put pointer
infile.seekg(10);
File pointers and their manipulations
Specifying the offset
seekg(offset, refposition);
seekp(offset, refposition);
The parameter offset represents the number of bytes the file pointer is to be moved
from the location specified by the parameter refposition.
Refpositions can from the following
ios::beg start of the file
ios::cur current position of the pointer
ios::end end of the file
fout.seekg(0, ios::beg) go to the start
fout.seekg(0, ios::cur) stay at the current position
fout.seekg(0, ios::end) go to the end of the file
fout.seekg(m, ios::beg) go to the m+1th postion
fout.seekg(-m, ios::cur) go backwards by m bytes from the current position

More Related Content

Similar to Files in C++.pdf is the notes of cpp for reference

Similar to Files in C++.pdf is the notes of cpp for reference (20)

Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with example
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handling
 
File Handling
File HandlingFile Handling
File Handling
 
Filehandling
FilehandlingFilehandling
Filehandling
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
File management in C++
File management in C++File management in C++
File management in C++
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
working with files
working with filesworking with files
working with files
 
Files in c++
Files in c++Files in c++
Files in c++
 
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
 
working with files
working with filesworking with files
working with files
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with files
 
Managing,working with files
Managing,working with filesManaging,working with files
Managing,working with files
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
 
cpp-file-handling
cpp-file-handlingcpp-file-handling
cpp-file-handling
 
Cpp file-handling
Cpp file-handlingCpp file-handling
Cpp file-handling
 

Recently uploaded

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
 
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
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 

Recently uploaded (20)

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
 
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
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 

Files in C++.pdf is the notes of cpp for reference

  • 2. Files and Stream classes Files the collection of records or programs of same nature. In C++, the concept of stream or stream classes to implement the I/O operations with the console and disk files. The I/O devices such as terminals, keyboards, disk etc. supplies an interface to the programmer for the smooth working with the system. This interface is known as stream. A stream is a sequence of bytes. It acts either as a source from which the input data can be obtained or a destination to which the output data can be sent. The source stream that provides data to the program is called the input stream and the destination stream that receives output from the program is known as output stream. cin and cout are used earlier in programs to perform this input and output operations
  • 3. Files and Stream classes ios ostream streambuf istream iostream istream_withassign Iostream_withassign ostream_withassign Input Pointer output
  • 4. Stream classes for console operations Class Name Contents ios Contains all basic facilities of input and output classes istream get(), getline(), read() and operator such as >> ostream put() and write(), insertion operator << iostream All input and output functions(ios/istream/ostream) streambuf It acts as a base for filebuf class used in files
  • 6. Unformatted I/O operations getline() and write() The getline() reads a whole line of text end with newline character(or when we press return(enter) key) cin.getline(line, size); Eg; char name[20]; cin.getline(name, 20); cout.write(line, size); The write() displays/prints a whole line of text
  • 7. Classes for file stream operations ios ostream streambuf istream iostream ifstream fstream ofstream Input Pointer output filebuf fstream base iostream.h file fstream.h file
  • 8. Details of file stream classes Class Contents filebuf Set the file buffers to read and write. It contains close() and open() as members fstreambase Provides operations common to the file streams, serves the base for ifsteram, ofstream, fstream classes. It contains open() and close() functions ifstream Provide input operations. Contains open() as default input mode. Inherits the functions get(), getline(), read(), seekg() and tellg() ofstream Provide output operations. Contains close() as default output mode. Inherits the functions put(), write(), seekp() and tellp() fstream Provides all input and output operations. It performs all functions of ifstream and ofstream. Inherits all istream and ostream classes through iostream
  • 9. Opening and closing a file • Suitable name for the file • Data type structure • Purpose • Opening method • Input.data • Test.doc • Student • output
  • 10. Opening and closing a file • Opening method 1. Using the constructor function of the class Here constructors are used in files to initialize the file stream object. 1. Choose appropriate filestream object 2. Initialize the object using suitable file name ofstream outfile(“results”); // this creates outfile as an ofstream object that manages the output stream This statement also opens the file results and attaches it to the output stream outfile Ifstream infile(“input.dat”); outfile<<“Total”; outfile<<sum; infile>> rollno; outfile.close(); //to close the outfile infile.close();
  • 11. Opening and closing a file • Opening method 1. Using the open() function for opening the file The function open() can be used to open multiple files that use the same stream object. file_stream_class stream_object; Stream_object.open(“filename”); ofstream outfile; outfile.open(“Data1”); …………………….. outfile.close(); outfile.open(“Data2”); …………………………… outfile.close();
  • 12. File Opening modes • Opening modes • It specifies the purpose of opening the file stream_object.open(“file_name”, mode); Mode specifies the purpose for which the file is opened. Default mode for ifstream is ios::in and for ofstream is ios::out Parameter Meaning ios::app Append to end-of-file ios::ate Go to the end-of-file on opening ios::binary Binary file ios::in Open file for reading only ios::nocreate Open fails if the file does not exists ios::noreplace Open fails if the file exists ios::out Open files for writing only ios::trunc Delete contents of the file if it exists
  • 13. File pointers and their manipulations Each file has two associated pointers known as file pointers. One is the input pointer (get pointer) and other one is output pointer (put pointer) We are using it for reading and writing the files seekg() Moves get pointer to a specified position seekp() Moves the put pointer to a specified position tellg() Gives the current position of the get pointer tellp() Gives the current position of the put pointer infile.seekg(10);
  • 14. File pointers and their manipulations Specifying the offset seekg(offset, refposition); seekp(offset, refposition); The parameter offset represents the number of bytes the file pointer is to be moved from the location specified by the parameter refposition. Refpositions can from the following ios::beg start of the file ios::cur current position of the pointer ios::end end of the file fout.seekg(0, ios::beg) go to the start fout.seekg(0, ios::cur) stay at the current position fout.seekg(0, ios::end) go to the end of the file fout.seekg(m, ios::beg) go to the m+1th postion fout.seekg(-m, ios::cur) go backwards by m bytes from the current position