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

Data file operations in C++ Base

  • 1.
  • 2.
     What isa 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 isfile..? 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 Thisdata 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 fileused for file operations fstream.h Data File operations
  • 6.
     OPENING ANDCLOSING 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 themember 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 Usingthe 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 STATEMEMBER 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()– checkwhether 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()– checkwhether 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()– checkwhether 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 FromA 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; charch; 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()– Usedto 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; charch; 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