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
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