0 ratings0% found this document useful (0 votes) 18 views4 pagesUnit 5 File Handling
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
)
oN P.R. Pote Patil 8203: OBJECT ORIENTED
ay College of Engineering & PROGRAMMING FOR PROBLEM
e Management, Amravati SOLVING
‘An Autonomous Institue Ailiated wo Sant Gadge Baba Amravati University
C++ Stream Clas
* InC++ stream refers to the stream of characters that are transferred between the program
thread andi/o.
= Stream classes in C++ are used to input and output operations on files and io devices.
‘These classes have specific features and to handle input and output of the program.
‘+ The iostream.h library holds all the stream classes in the C++ programming language.
ios class ~ This class is the base class for all stream classes. The streams can be input or output streams.
‘This class defines members that are independent of how the templates of the class are defined.
ostream class ~ The ostream class handles the output stream in c++ programming language. These
output stream objects are used to write data as a sequence of characters on the screen. cout and puts
handle the out streams in c++ programming language.
Example 1:
#include
using namespace std;
int main0{
cout<<"This output is printed on screen";
}
Output:
This output is printed on screen
Example 2:
#include
using namespace std;
int main({
puts("This output is printed using puts’
}
Output:
This output is printed using puts
Dr. Zeeshan I. Khan, Assistant Professor, Dept. of CSE
[Contact: 8380008700, Email: zikhan@prpotepatilengg.ac.in]a)
ie | P.R. Pote Patil ©8203: OBJECT ORIENTED
omy College of Engineering & PROGRAMMING FOR PROBLEM
i Management, Amravati SOLVING
‘An Autonomous Institue Ailiated wo Sant Gadge Baba Amravati University
istream Class - The istream class handles the input stream in c++ programming language. These input
stream objects are used to read and interpret the input as a sequence of characters. The cin handles the
input.
Example 1:
#include
using namespace std;
int main((
int no;
cout<<"Enter a number";
cin>>no;
cout<<"Number entered using cin is "<
using namespace sid;
int main
char ch{10];
puts("Enter a character array");
gets(ch);
puts("The character array entered using gets is :
puts(ch);
}
Output:
Enter a character array
thdgt
‘The character array entered using gets is :
thdgt
Dr. Zeeshan I. Khan, Assistant Professor, Dept. of CSE
[Contact: 8380008700, Email: zikhan@prpotepatilengg.ac.in]a)
ae
P.R. Pote Patil €S203: OBJECT ORIENTED
College of Engineering & PROGRAMMING FOR PROBLEM
Management, Amravati SOLVING
‘An Autonomous Institue Ailiated wo Sant Gadge Baba Amravati University
Opening, Closing, Reading & Writing a File
Files play an important role in programming.
It allows storage of data permanently.
The C++ language provides a mechanism to store the output of a program in a file and browse
from a file on the disk.
This mechanism is termed file handling.
In order to perform file handling, some general functions which are used are as follows:
oR ep
open(): This function helps to create a file and open the file in different modes, like input
operations, output operations, binary mode, etc.
close(): This function helps to close an existing file.
get(): This function helps to read a single character from the file.
put(): This function helps to write a single character in the file.
read(): This function helps to read data from a file.
write(): This function helps us to write data into a file.
For Example
#include
#finclude
using namespace std;
int main
{
char data[100};
ofstream outtile;
outfile.open("trial.txt");
cout << "Writing to the file" << endl;
cout << "Enter your name: ";
cin.getline(data, 100);
outfile << data << endl;
outfile.close();
ifstream infile;
infile.open("trial.txt");
cout << "Reading from the file"<< endl;
infile >> data;
cout << data << endl;
infile.closeQ;
return 0;
}
Output:
Writing to the file
Enter your name: Hello
Reading from the file
Hello
Dr. Zeeshan I. Khan, Assistant Professor, Dept. of CSE
[Contact: 8380008700, Email: zikhan@prpotepatilengg.ac.in])
oN P.R. Pote Patil
ay Collage of Engineering &
u Management, Amravati
‘An Autonomous Institue Ailiated wo Sant Gadge Baba Amravati University
End of File in C++
The eof() method of ios class in C++ is used to check if the stream is has raised any EOF (End Of
File) error.
©8203: OBJECT ORIENTED
PROGRAMMING FOR PROBLEM
SOLVING
It means that this function will check if this stream has its eofbit set.
Syntax:
bool eof() con:
Example:
#tinclude
using namespace std;
int main
{
stringstream ss;
bool isEOF = ss.cof();
cout << "is stream eof: "<< isEOF << endl;
ss.clear(ss.eotbit);
isEOF = ss.eof();
cout << "is stream eof: "<< isEOF << endl;
return 0;
}
Output:
is stream eof: 0
is stream eof: 1
Dr. Zeeshan I. Khan, Assistant Professor, Dept. of CSE
[Contact: 8380008700, Email: zikhan@prpotepatilengg.ac.in]