0% found this document useful (0 votes)
31 views

Data File Handling

1) This document discusses file handling in C++ through the use of file streams. File streams allow programs to read and write data to files for permanent storage. 2) There are different types of file streams for input and output. Files can be opened using constructor functions or member functions and specifying the file mode determines if it is opened for reading, writing, or both. 3) Various member functions allow checking the state of file streams and reading or writing data. Data can be read from and written to files using extraction, insertion, get/put, or read/write functions.

Uploaded by

Mani
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Data File Handling

1) This document discusses file handling in C++ through the use of file streams. File streams allow programs to read and write data to files for permanent storage. 2) There are different types of file streams for input and output. Files can be opened using constructor functions or member functions and specifying the file mode determines if it is opened for reading, writing, or both. 3) Various member functions allow checking the state of file streams and reading or writing data. Data can be read from and written to files using extraction, insertion, get/put, or read/write functions.

Uploaded by

Mani
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Data File Handling

Introduction

• Computer programs are associated to work


with files as it helps in storing data &
information permanently.
• File - itself a bunch of bytes stored on some
storage devices.
• In C++ this is achieved through a component
header file called fstream.h
Contd…

• Earlier, only two streams are being used such as cin


and cout.
• There are some problems with these streams.
1) It is very difficult to handle large volume of data.
2) The data is not permanently stored.
Eg:- the calculation of the result of 200 students will
be lost when the program terminates.
Contd…..

• C++ provides a new technique for handling I/O


operations through mechanism known as streams.
• A stream refers to a flow of data.
• Classified in 2 categories:
1. Output stream
2. Input stream
In output stream flow of data is from program to the
output device.
In input stream the flow of data is from input device
to a program in main memory.
File  Program ( Input stream) - reads
Program  File (Output stream) – write
Stream Class Hierarchy
Why to use Files:

• Convenient way to deal large quantities of data.


• Store data permanently (until file is deleted).
• Avoid typing data into program multiple times.
• Share data between programs.

We need to know:
how to "connect" file to program
how to tell the program to read data
how to tell the program to write data
error checking and handling EOF
• A file can be opened in two ways:
1. Using constructor function
2. Using member function
Opening file using constructor

• File name is used to initialize the file stream object.


• Steps:-
1. Create a file stream object to manage stream with
appropriate class.
2. Initialize the file object with desired name.
Contd……..

• ofstream outfile(“results”); //it opens a file name


“result” for output.

• ifstream infile(“data”);
outfile<<“total”;
outfile.close();
infile>> string;
Opening files using member function
open()
• This function is used to open multiple files that uses
same stream object.
• Syntax:-
file_stream class stream_object;
stream_object.open(“filename”);
Example
ofstream outfile;
outfile.open(“Data”);
…………………..
…………………..
outfile.close();
outfile.open(“Data2”);
…………………….
…………………..
outfile.close();
Open(): File Modes

• We can have two arguments in open(), to specify


the file-mode.

stream-object.open(“filename”,mode);

the second argument specifies the purpose for which


file is opened.
Stream state member functions

• In C++, file stream classes inherit a stream state


member from the ios class, which gives out the
information regarding the status of the stream.
For e.g.:
eof() –used to check the end of file character
fail()- used to check the status of file at
opening for I/O
bad()- used to check whether invalid file
operations or unrecoverable error .
good()- used to check whether the previous file
operation has been successful
EXAMPLES

• EOF()
Void main()
{
Ifstream infile;
Infile.open(“text”);
While(!infile.eof())
{
------
-----
}}
• FAIL()
Main()
{
Ifstream infile;
Infile.open(“text”);
While(!infile.fail())
{
Cout<<“cudn’t open a file”;
}}
Reading and Writing in Files

• Reading and Writing a character from a file can be


done by
• Get()
• Put()
• Get():- This is used to read an alphanumeric
character from a specified file.
• Put():- This is used to write an alphanumeric
character to specified file.
Reading and writing by insertion and
extraction
Stream Insertion Operators
• Are defined in the ostream class
• The operator “<<” is called the inserter

Stream Extraction Operators


• Are defined in the istream class and are used to
receive data from the input device
• The operator “>>”, called the extractor.
By Read () and Write()

• Read and write is used when we are dealing with


classes.
• Syntax:-
Read((char*)&obj sizeof(obj));
Write((char*)&obj,sizeof(obj));
EXAMPLE
MEMBER FUNCTIONS

• ios::app = append at end of file


• Ios::ate = go to end of file on opening instead of
beginning.
• Ios::binary=binary file
• Ios::in = open file for reading only
• Ios::out = open file for writing only
• Ios:: trunc = delete the content of file if it exists
• Ios::nocreate = open fails if file doesn’t exist.
EXAMPLE

1) ofstream fileout;
fileout.open(“hello”,ios::app);
2)fileout.open(“hello”, ios::in | ios::out);

You might also like