SlideShare a Scribd company logo
1 of 18
BY
ARJUN M
 What is a file..?
 File Streams
 File operations
 Opening of a file
 Closing of a file
 File Modes
 Stream State Member Functions
 Reading/Writing a Character from a
file
Data File operations
 What is file..?
File is a collection of data or a set of characters or may be
a text or a program.
 Types of files
i) Sequential files.
ii) Random access files.
Data File operations
File Streams
 ofstream
This data type represents the output file
stream and is used to create files and to write
information to files.
 ifstream
This data type represents the input file stream
and is used to read information from files.
 fstream
This data type represents the file stream generally,
and has the capabilities of both ofstream and ifstream
which means it can create files, write information to
files, and read information from files.
Data File operations
 Header file used for file operations
fstream.h
Data File operations
 OPENING AND CLOSING OF FILES
 Opening a File.
i) ifstream – To read a stream of objects from a file.
eg:
#include<fstream.h>
void main()
{
ifstream infile; // creating class object
infile.open(“data_file”); // opening a file
……………………………
}
Data File operations
ii) ofstream – To write a stream of objects on a file.
eg:
#include<fstream.h>
void main()
{
ofstream infile; // creating class object
infile.open(“data_file”); // opening a file
……………………………
}
Data File operations
ii) fstream – Both read and write.
eg:
#include<fstream.h>
void main()
{
fstream infile; // creating class object
infile.open(“data_file”, ios::in|| ios::out // opening a file
……………………………
}
Data File operations
Name of the member function Meaning
ios :: in Open a file for reading
ios :: out Open a file for writing
ios :: app Append at the end of a file
ios :: ate Seek to the end of a file upon opening
Instead of beginning
ios :: trunc Delete a file if it exists and recreate it
ios :: nocreate Open a file if a file does not exist
ios :: replace Open a file if a file does exist
Data File operations
ios :: binary Open a file for binary mode; default is
text
File Modes
Closing a File
Using the function close(); with no arguments.
eg:
#include<fstream.h>
void main()
{
ofstream infile;
infile.open(“data_file”);
……
infile.close(); // calling to close the file
}
Data File operations
 STREAM STATE MEMBER FUNCTIONS
Information status of file.
Returns a Binary value
i) eof() – check whether the file pointer has reached the end
of file or not.
eg:
#include<fstream.h>
void main()
{
ifstream infile;
infile.open(“text”);
while(!infile.eof())
{……………………………..}
}
Data File operations
ii) fail()– check whether the file has been opened for i/o
successfully
eg:
#include<fstream.h>
void main()
{
ifstream infile;
infile.open(“text”);
while(!infile.fail())
{ cout<<“Couldn’t open a file”;
……………………………..}
}
Data File operations
iii) bad()– check whether any invalid file operations.
eg:
#include<fstream.h>
void main()
{
ifstream infile;
infile.open(“text”);
if(infile.bad())
{ cout<<“open failure”;
……………………………..}
}
Data File operations
iv) good()– check whether the previous file operation
has been successful or not..
eg:
#include<fstream.h>
void main()
{
ifstream infile;
infile.open(“text”);
if(infile.good())
{
……………………………..}
}
Data File operations
Reading/Writing A Character
From A File
i) get()– Used to read an alphanumeric character from a
file.
eg:
#include<fstream.h>
void main()
{
ifstream infile;
char ch;
infile.open(“text”);
while(!infile.eof())
{
ch=infile.get();
……………….
}
Data File operations
#include<fstream.h>
#include<conio.h>
void main()
{
ifstream fin;
char ch;
fin.open("demo.txt");
cout<<"nData in file...";
while(!fin.eof())
{
fin.get(ch); // get character
cout<<ch; // display a character onto screen
}
fin.close();
}
Output :
Data in file... Hello friends, my name is arJun.
Data File operations
ii) put()– Used to write a character to a specified
file/output stream.
eg:
#include<fstream.h>
void main()
{
ofstream outfile;
char ch;
outfile.open(“text”);
while(!infile.eof())
{
ch=outfile.get();
cout.put(ch); // display a character onto screen
……………
}
Data File operations
#include<fstream.h
#include<conio.h>
void main()
{
ofstream fout;
char ch;
fout.open("demo.txt");
do
{
cin.get(ch); // get character
fout.put(ch); // writing to file
}while(ch!=EOF);
fout.close();
cout<<"nData written successfully...";
}
Output :
Hello friends, my name is kumar.
Data written successfully...
Data File operations

More Related Content

What's hot (20)

Switching & Multiplexing
Switching & MultiplexingSwitching & Multiplexing
Switching & Multiplexing
 
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary fileCBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
 
Files in php
Files in phpFiles in php
Files in php
 
Php files
Php filesPhp files
Php files
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
C files
C filesC files
C files
 
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
 
File Management in C
File Management in CFile Management in C
File Management in C
 
File in c
File in cFile in c
File in c
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operations
 
Filehandling
FilehandlingFilehandling
Filehandling
 
File handling
File handlingFile handling
File handling
 
File Management
File ManagementFile Management
File Management
 
File Management in C
File Management in CFile Management in C
File Management in C
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
Python data file handling
Python data file handlingPython data file handling
Python data file handling
 
File handling in C
File handling in CFile handling in C
File handling in C
 
File handling-c
File handling-cFile handling-c
File handling-c
 
Files in C
Files in CFiles in C
Files in C
 

Similar to Data file operations in C++ Base

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 SYLLABUSVenugopalavarma Raja
 
pointer, structure ,union and intro to file handling
 pointer, structure ,union and intro to file handling pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operationsTAlha MAlik
 
Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.2 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.2 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handlingRai University
 
File handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxFile handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxarmaansohail9356
 
File Management and manipulation in C++ Programming
File Management and manipulation in C++ ProgrammingFile Management and manipulation in C++ Programming
File Management and manipulation in C++ ProgrammingChereLemma2
 
FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.ssuser00ad4e
 

Similar to Data file operations in C++ Base (20)

Filesin c++
Filesin c++Filesin c++
Filesin c++
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
 
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
 
pointer, structure ,union and intro to file handling
 pointer, structure ,union and intro to file handling pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
 
Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.2 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.2 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handling
 
File handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxFile handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptx
 
File handling
File handlingFile handling
File handling
 
File operations
File operationsFile operations
File operations
 
File handling in c
File handling in cFile handling in c
File handling in c
 
data file handling
data file handlingdata file handling
data file handling
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
File handling in cpp
File handling in cppFile handling in cpp
File handling in cpp
 
File Management and manipulation in C++ Programming
File Management and manipulation in C++ ProgrammingFile Management and manipulation in C++ Programming
File Management and manipulation in C++ Programming
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
File Handling in C
File Handling in CFile Handling in C
File Handling in C
 
FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.FILE HANDLING in python to understand basic operations.
FILE HANDLING in python to understand basic operations.
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
 
File Handling
File HandlingFile Handling
File Handling
 

Recently uploaded

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
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
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
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
 
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
 
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
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
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
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
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
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 

Recently uploaded (20)

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
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
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
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
 
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
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
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...
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
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
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
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
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 

Data file operations in C++ Base

  • 2.  What is a file..?  File Streams  File operations  Opening of a file  Closing of a file  File Modes  Stream State Member Functions  Reading/Writing a Character from a file Data File operations
  • 3.  What is file..? File is a collection of data or a set of characters or may be a text or a program.  Types of files i) Sequential files. ii) Random access files. Data File operations
  • 4. File Streams  ofstream This data type represents the output file stream and is used to create files and to write information to files.  ifstream This data type represents the input file stream and is used to read information from files.  fstream This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files. Data File operations
  • 5.  Header file used for file operations fstream.h Data File operations
  • 6.  OPENING AND CLOSING OF FILES  Opening a File. i) ifstream – To read a stream of objects from a file. eg: #include<fstream.h> void main() { ifstream infile; // creating class object infile.open(“data_file”); // opening a file …………………………… } Data File operations
  • 7. ii) ofstream – To write a stream of objects on a file. eg: #include<fstream.h> void main() { ofstream infile; // creating class object infile.open(“data_file”); // opening a file …………………………… } Data File operations
  • 8. ii) fstream – Both read and write. eg: #include<fstream.h> void main() { fstream infile; // creating class object infile.open(“data_file”, ios::in|| ios::out // opening a file …………………………… } Data File operations
  • 9. Name of the member function Meaning ios :: in Open a file for reading ios :: out Open a file for writing ios :: app Append at the end of a file ios :: ate Seek to the end of a file upon opening Instead of beginning ios :: trunc Delete a file if it exists and recreate it ios :: nocreate Open a file if a file does not exist ios :: replace Open a file if a file does exist Data File operations ios :: binary Open a file for binary mode; default is text File Modes
  • 10. Closing a File Using the function close(); with no arguments. eg: #include<fstream.h> void main() { ofstream infile; infile.open(“data_file”); …… infile.close(); // calling to close the file } Data File operations
  • 11.  STREAM STATE MEMBER FUNCTIONS Information status of file. Returns a Binary value i) eof() – check whether the file pointer has reached the end of file or not. eg: #include<fstream.h> void main() { ifstream infile; infile.open(“text”); while(!infile.eof()) {……………………………..} } Data File operations
  • 12. ii) fail()– check whether the file has been opened for i/o successfully eg: #include<fstream.h> void main() { ifstream infile; infile.open(“text”); while(!infile.fail()) { cout<<“Couldn’t open a file”; ……………………………..} } Data File operations
  • 13. iii) bad()– check whether any invalid file operations. eg: #include<fstream.h> void main() { ifstream infile; infile.open(“text”); if(infile.bad()) { cout<<“open failure”; ……………………………..} } Data File operations
  • 14. iv) good()– check whether the previous file operation has been successful or not.. eg: #include<fstream.h> void main() { ifstream infile; infile.open(“text”); if(infile.good()) { ……………………………..} } Data File operations
  • 15. Reading/Writing A Character From A File i) get()– Used to read an alphanumeric character from a file. eg: #include<fstream.h> void main() { ifstream infile; char ch; infile.open(“text”); while(!infile.eof()) { ch=infile.get(); ………………. } Data File operations
  • 16. #include<fstream.h> #include<conio.h> void main() { ifstream fin; char ch; fin.open("demo.txt"); cout<<"nData in file..."; while(!fin.eof()) { fin.get(ch); // get character cout<<ch; // display a character onto screen } fin.close(); } Output : Data in file... Hello friends, my name is arJun. Data File operations
  • 17. ii) put()– Used to write a character to a specified file/output stream. eg: #include<fstream.h> void main() { ofstream outfile; char ch; outfile.open(“text”); while(!infile.eof()) { ch=outfile.get(); cout.put(ch); // display a character onto screen …………… } Data File operations
  • 18. #include<fstream.h #include<conio.h> void main() { ofstream fout; char ch; fout.open("demo.txt"); do { cin.get(ch); // get character fout.put(ch); // writing to file }while(ch!=EOF); fout.close(); cout<<"nData written successfully..."; } Output : Hello friends, my name is kumar. Data written successfully... Data File operations