FILE STREAM
A Project Report
Seminar Report submitted in partial fulfilment of the requirements for the
PART of the degree of B.E. in Electrical and Electronic engineering under
University of Technology
By
RAMYASHREE V
USN : 2LG21CS033
Under the Guidance of
Miss.Shridevi
( Dept of Computer Science and Engineering )
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
GOVERNMENT ENGINEERING COLLEGE,
TALAKAL, KOPPALA.
TABLE CONTENT
CERTIFICATE
ACKNOWLEDGEMENT
APPROVAL
INTRODUCTION
CLASSES OF FILE STREAM
EXAMPLE PROGRAM
BENEFITS OF FILE STREAM
CONCLUSION
REFERENCES
CERTIFICATE
This is to certify that the Seminar entitled About File Stream presented by
Ramyashree V bearing USN 2LG21CS037 of Computer Science and
Engineering has been has been completed successfully.
This is in partial fulfilment of the requirements part of Bachelor Degree in
Computer Science and Engineering, GOVERNMENT ENGINEERING
COLLEGE TALKAL, KOPPAL.
Under Affiliated to Visvesvaraya Technological University, Belagavi
I wish her/ him success in all future endeavors.
Prof. Ramakrishna Metri
(Asst./Asso./Prof., Department of Mathematics Engineering)
ACKNOWLEDGMENTS
I would like to express my deep and sincere gratitude to my guide(s), Prof
Ramakrishna metri sir of mathematics engineering for his/her/their unflagging
support and continuous encouragement throughout the seminar. Without
his/her/their guidance and persistent help this report would not have been
possible.
I must acknowledge the faculties and staffs of Engineering ,I sincerely thanks a
gratitude to my teacher Miss Shridevi, I also wish to express the staff
members.HOD for rendering all possible help and support during development,
implementation and presentation of the seminar.
Its my great pleasure to acknowledge my colleagues for providing this
opportunity I am especially grateful to respective teachers.
Ramyashree V
Department of Computer and Science Engineering
USN:2LG21CS033
ABSTRACT
Stream in C++ means a stream of characters that gets transferred between the
program thread and input or output. There are a number of C++ stream classes
eligible and defined which is related to the files and streams for providing input-
output operations. All the classes and structures maintaining the file and folders
with hierarchies are defined within the file with iostream.h standard library.
Classes associated with the C++ stream include ios class, istream class, and
ostream class. Class ios is indirectly inherited from the base class involving
iostream class using istream class and ostream class which is declared virtually.
Signature of the Student
Name : Ramyashree V
USN : 2LG21CS033
Semester : 3rd sem
Branch : Computer science and engineering
Section:
Date:
INTRODUCTION
In C++ the concept of the file stream is used for the reading and writing on the
file system. In very simple and technical words we can say it has ability to do
dual work which means it has of stream and if stream. So in case if the file is
not there on which we are going to write some contents then the f stream has
capability to write the contents on the file and at the same time it also allows us
to open the file and display the contents of the files. We should use this if we
know that we are going to create, read and write contents on the file.
Classes for File stream operations :-
The I/O system of C++ contains a set of classes which define the file handling
methods. These include ifstream, ofstream and fstream classes. These classes
are derived from fstream and from the corresponding iostream class. These
classes, designed to manage the disk files, are declared in fstream and
therefore we must include this file in any program that uses files.
1. ios:-
ios stands for input output stream.
This class is the base class for other classes in this class hierarchy.
This class contains the necessary facilities that are used by all the other
derived classes for input and output operations.
2. istream:-
istream stands for input stream.
This class is derived from the class ‘ios’.
This class handle input stream.
The extraction operator(>>) is overloaded in this class to handle input
streams from files to the program execution.
This class declares input functions such as get(), getline() and read().
3. ostream:-
ostream stands for output stream.
This class is derived from the class ‘ios’.
This class handle output stream.
The insertion operator(<<) is overloaded in this class to handle output
streams to files from the program execution.
This class declares output functions such as put() and write().
4. streambuf:-
This class contains a pointer which points to the buffer which is used to
manage the input and output streams.
5. fstreambase:-
This class provides operations common to the file streams. Serves as a base
for fstream, ifstream and ofstream class.
This class contains open() and close() function.
6. ifstream:-
This class provides input operations.
It contains open() function with default input mode.
Inherits the functions get(), getline(), read(), seekg() and tellg() functions
from the istream.
7. ofstream:-
This class provides output operations.
It contains open() function with default output mode.
Inherits the functions put(), write(), seekp() and tellp() functions from the
ostream.
8. fstream:-
This class provides support for simultaneous input and output operations.
Inherits all the functions from istream and ostream classes through
iostream.
9. filebuf:-
Its purpose is to set the file buffers to read and write.
We can also use file buffer member function to determine the length of the
file.
In C++, files are mainly dealt by using three classes fstream, ifstream,
ofstream available in fstream headerfile.
ofstream: Stream class to write on files.
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
Syntax:
Below is a simple syntax for the f stream in the C++. In the below example first
we are getting or creating a file, we can give any name to file which we are
creating here. Second we are writing some contents to the file. In the same way
we can read the file content with help of the get line function in while loop.
Of stream creatMyFile(AnyFileName);
creatMyFile << Any text as contents;
Data Type Description
fstream It is used to create files, write information to files, and read information from files.
ifstream It is used to read information from files.
ofstream It is used to create files and write information to the files.
How file stream work in C++?
We have already package like if stream and of stream but they can either
read and write the file, but what we do in case if we want to perform read and
write the file? So for that case we have f stream C++ package.
We can create a file if file does not exists like.
Here first we can create a file instance with code like “of stream of”, here
of will be used as the instance.
Next we can pass any name of file which we want to create like
“open(any filename);”.
Finally, we can write the contents on the file like cout << “any contents
and text data” << endl;
If needed, then we can also read the contents of the file with the help of
the functions of get line to read data line by line.
EXAMPLE1:
//Importing the package io_stream
#include <iostream>
//Importing the package fstream
#include <fstream>
//Importing the string package for string related works
#include <string>
using namespace std;
int main () {
string ln;
//Creating a file with name test.txt ,if not exists
ifstream testFile ("test.txt");
//Checking the file opening condition
if (testFile.is_open())
{
//Running a while loop and fetch all the contents line by line
while ( getline (testFile,ln) )
{
//Printing the output of the file contents
cout << ln << '\n';
}
//Here we are closing the opened file
testFile.close();
}
else cout << "File is not there on the given path";
return 0;
}
Opening a File
A file must be opened before you can read from it or write to it.
Either ofstream or fstream object may be used to open a file for writing. And
ifstream object is used to open a file for reading purpose only.
Following is the standard syntax for open() function, which is a member of
fstream, ifstream, and ofstream objects.
void open(const char *filename, ios::openmode mode);
Here, the first argument specifies the name and location of the file to be opened
and the second argument of the open() member function defines the mode in
which the file should be opened.
Closing a File
When a C++ program terminates it automatically flushes all the streams, release
all the allocated memory and close all the opened files. But it is always a good
practice that a programmer should close all the opened files before program
termination.
Following is the standard syntax for close() function, which is a member of
fstream, ifstream, and ofstream objects.
void close();
Writing to a File
While doing C++ programming, you write information to a file from your
program using the stream insertion operator (<<) just as you use that operator
to output information to the screen. The only difference is that you use
an ofstream or fstream object instead of the cout object.
Reading from a File
You read information from a file into your program using the stream extraction
operator (>>) just as you use that operator to input information from the
keyboard. The only difference is that you use an ifstream or fstream object
instead of the cin object.
Read and write example:
#include<io_stream.h>
Void main()
{
int p,q,r;
Cout<<”Enter 3 integers separated by space:\n”;
Cin>>”Sum of the”<<p<<”,”<<q<<”and”<<r<<”is=”<<(p+q+r)<<endl;
}
Advantages of C++ file stream
Simple to Use. There are no formatting characters (such as %d) in
streams, since each object already knows how to display itself .
This removes a major source of errors.
Existing operators and functions can be overloaded.
For Ex-the insertion (<<) and extraction (>>) operators.
This makes your own classes work in the same way as the built in
types, which again makes programming easier .
One of the most important things about it is, it allows us to use the
concept of internalization and localization.
It gives us a complete object oriented approach. Because of which we can
reuse the features many times.
Because it has a feature where if the file does not exist instead of
throwing an error it will create the file for us.
CONCLUSION
C++ Stream is a very powerful and versatile functionality of the stream classes.
They provide programmers an insight for using the predefined and in build
functions by modification in the object and the standard libraries of the class for
various manipulations and arrangements of the files and folders thus
maintaining the hierarchical structure intact for the C++ stream.
References:
1) Object oriented programming with C++ E Balaguruswamy
2) Object oriented programming using C++ Dr. Rajiv Chopra
3 ) Object oriented programming using C++ A.M. PADMA REDDY
Cloud Computing
Hadoop
ReactJS
Data Science
Angular 7
Blockchain
Git
Machine Learning
DevOps
B.Tech / MCA
DBMS
Data Structures
DAA
Operating System
Computer Network
Compiler Design
Computer Organization
Discrete Mathematics
Ethical Hacking
Computer Graphics
Software Engineering
Web Technology
Cyber Security
Automata
C Programming
C++
Java
.Net
Python
Programs
Control System
Data Mining
Data Warehouse
Report this ad
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more
information about given services.
o Website Designing
o Website Development
o Java Development
o PHP Development
o WordPress
o Graphic Designing
o Logo
o Digital Marketing
o On Page and Off Page SEO
o PPC
o Content Development
o Corporate Training
o Classroom and Online Training
o Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android,
Hadoop, PHP, Web Technology and Python. Please mail your requirement
at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
LEARN TUTORIALS
Learn JavaLearn Data StructuresLearn C ProgrammingLearn C++ TutorialLearn C# TutorialLearn PHP
TutorialLearn HTML TutorialLearn JavaScript TutorialLearn jQuery TutorialLearn Spring Tutorial
OUR WEBSITES
Javatpoint.comHindi100.comLyricsia.comQuoteperson.comJobandplacement.com
OUR SERVICES
Website Development
Android Development
Website Designing
Digital Marketing
Summer Training
Industrial Training
College Campus Training
CONTACT
Address: G-13, 2nd Floor, Sec-3
Noida, UP, 201301, India
Contact No: 0120-4256464, 9990449935
Contact UsSubscribe UsPrivacy PolicySitemap
About Me
© Copyright 2011-2021 www.javatpoint.com. All rights reserved. Developed by JavaTpoint.
Why do you want to report this ad?
EMAIL (OPTIONAL)
Report This Ad
X
Feedback
Help Others, Please Share
Learn Latest Tutorials
Splunk
SPSS
Swagger
Transact-SQL
Tumblr
ReactJS
Regex
Reinforcement Learning
R Programming
RxJS
React Native
Python Design Patterns
Python Pillow
Python Turtle
Keras
Preparation
Aptitude
Reasoning
Verbal Ability
Interview Questions
Company Questions
Trending Technologies
Artificial Intelligence
AWS
Selenium
Cloud Computing
Hadoop
ReactJS
Data Science
Angular 7
Blockchain
Git
Machine Learning
DevOps
B.Tech / MCA
DBMS
Compiler Design
Computer Organization
Discrete Mathematics
Ethical Hacking
Computer Graphics
Software Engineering
Web Technology
Cyber Security
Automata
C Programming
C++
Java
.Net
Python
Programs
Control System
Data Mining
Data Warehouse
Report this ad
Javatpoint Services