FPCPP C5
FPCPP C5
FUNDAMENTALS OF
5
PROGRAMMING IN C++
FILE OPERATIONS
IO-operations 2
Input can be taken from the keyboard or from a file.
It delivered to your program via a C++ construct known as a stream.
Similarly, output can be sent to the screen or to a file.
and output from your program is delivered to the output device via a stream.
C++ standard IO is based on three classes:
the istream class for input,
the ostream class for output,
and the iostream class for input/output.
C++ refers to files as streams since it considers them a stream of bytes.
Streams 3
A stream is a flow of characters (or other kind of data).
If the flow is into your program, the stream is called an input stream.
If the flow is out of your program, the stream is called an output stream.
Example: cin is an input stream and cout is an output stream objects defined in iostream
library.
Why file? 4
• The keyboard input and screen output support temporary data.
• The data will be erased (or lost) when the program ends.
• Files provide you with a way to store data permanently.
• The content of a file will remain after the program has finished running.
• File IO supports:
• large volume of data,
• over and over again use of input data by many programs without the need to type in the data
separately for each program.
• When your program :
• takes input from a file, it is said to be reading from the file.
• sends output to a file, it is said to be writing to the file.
File IO 5
• In C++, a stream is a special kind of variable known as an object.
• Stream objects handles inputs to and outputs from a program whatever the source and destination
will be.
• If you want to use a stream to get input from a file (or give output to a file), you must
declare the stream and you must connect the stream to the file.
Declaring streams 6
• The streams cin and cout are already declared for you, but if you want a stream to connect
to a file,
• Stream must be declare just as you would declare any other variable.
• The type for input-file stream variables is named ifstream (for “input-file stream”).
• Ifstream objects used for opening file to read.
• The type for output-file stream variables is named ofstream (for “output-file stream”).
• Ofstream objects used for opening file to write.
• The type for both input and output file stream variable is named fstream (for “file stream”).
• Fstream objects used for opening file to read from and write to a file at the same time.
… 7
Example:
ifstream readfile;
// declaring an input-file stream object called readfile to open file to read input data into our program.
ofstream writefile;
// declaring an output-file stream object called writefile to open file to write output data from our
program.
The types ifstream and ofstream are defined in the library with the header file fstream,
So to use them we must include #include<fstream> directive in the program,
Note after declaring the stream variables(objects), we must connect them to the actual file
from our disk.
Connecting streams to a file 8
• Stream variables, such as readfile and writefile declared earlier, must each be connected to
a file.
• This is called opening the file and is done with a function named open.
• For example, suppose you want the input/output stream connected to the file named
myfile.txt.
• The connection can be done using open function,
fstream rwfile; // declaration of input-file stream.
rwfile.open(“myfile.txt”); // connecting a stream to the actual file.
• Or a constructor
fstream rwfile(“myfile.txt”); // the connection done in one line.
Opening file to read 9
• We must use ifstream object to open file as an input to( to read data for) the program.
• When we open a file using ifstream object (to read):
• The compiler will cause an error if the file we mentioned doesn’t exist in place.
• if(!readfile.eof())
Opening file to write 11
• We must use ofstream object to open file to write data (output) from the program
permanently.
• When we open a file using ofstream object (to write):
• The compiler will automatically create it, if it doesn’t exist in place.
• Or if the file exists it automatically starts overwriting it unless opened in append mode.
• writefile.close() or readfile.close()
File Pointers
Each file has two pointers
one is called input pointer
and second is output pointer.
The input pointer is called get pointer and the output pointer is called put pointer.
When input and output operation take places, the appropriate pointer is automatically set
according to the access mode.
For example:
when we open a file in reading mode, file pointer is automatically set to the start of the file.
when we open a file in append mode, the file pointer is automatically set to the end of file
…
In C++ there are some manipulators by which we can control the movement of the pointer. The
available manipulators are:
seekg() tellp(): this gives the current position of put
seekp() pointer (output pointer) eg. ofstream fileout;
fileout.open(“c:\\
tellg()
test.txt”,ios::app);
tellp() int length = fileout.tellp();
seekg(): this moves get pointer i.e. input pointer to a specified location.
For e.g. infile.seekg(5); move the file pointer to the byte number 5 from starting point.
seekp(): this move put pointer (output pointer) to a specified location.
For example: outfile.seekp(5);
tellg(): this gives the current position of get pointer (input pointer)
Special operations in a File 15
put() - It writes a single character to file.
Note: For seekp and seekg three reference points are passed:
ios::beg - beginning of the file
ios::cur - current position in the file
ios::end - end of the file
16
Example:
cout.setf(ios::scientific);
cout.precision(2);
// this makes decimal numbers to be displayed in e-notations.
cout<<0.0004563<<":"<<endl; // 4.56e-004:
… 19
… 20
Predefined character functions 21
… 22
… 23