SlideShare a Scribd company logo
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

Switching & Multiplexing
Switching & MultiplexingSwitching & Multiplexing
Switching & Multiplexing
MelkamuEndale1
 
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
ShivaniJayaprakash1
 
Files in php
Files in phpFiles in php
Files in php
sana mateen
 
Php files
Php filesPhp files
Php files
kalyani66
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
ٖFaiXy :)
 
C files
C filesC files
File in C Programming
File in C ProgrammingFile in C Programming
File in C Programming
Sonya Akter Rupa
 
File Management in C
File Management in CFile Management in C
File Management in C
Munazza-Mah-Jabeen
 
File in c
File in cFile in c
File in c
Prabhu Govind
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operations
Jamshid Hashimi
 
File handling
File handlingFile handling
File handling
SabahtHussein
 
File Management
File ManagementFile Management
File Management
Ravinder Kamboj
 
File Management in C
File Management in CFile Management in C
File Management in C
Paurav Shah
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
Python data file handling
Python data file handlingPython data file handling
Python data file handling
ToniyaP1
 
File handling in C
File handling in CFile handling in C
File handling in C
Kamal Acharya
 
File handling-c
File handling-cFile handling-c
Files in C
Files in CFiles in C
Files in C
Prabu U
 

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

Filesin c++
Filesin c++Filesin c++
Filesin c++
HalaiHansaika
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
ARYAN552812
 
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
Venugopalavarma 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 handling
Rai University
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
Mehul Desai
 
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 handling
Rai University
 
File handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxFile handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptx
armaansohail9356
 
File operations
File operationsFile operations
File operations
PrabhatKumarChaudhar2
 
data file handling
data file handlingdata file handling
data file handling
krishna partiwala
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
Praveen M Jigajinni
 
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
ChereLemma2
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
Emertxe Information Technologies Pvt Ltd
 
File Handling in C
File Handling in CFile Handling in C
File Handling in C
VrushaliSolanke
 
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
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
MikialeTesfamariam
 
File Handling
File HandlingFile Handling
File Handling
AlgeronTongdoTopi
 

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

Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 

Recently uploaded (20)

Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 

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