2. The file is the many data are stored
in the particular place in the disk.
The many methods are available for
storing in the file and retrieving
from the file.
3. Input stream
The input stream is use to supplies data
in the program.
Output stream
The output stream is use to receives
data from the file.
streams
4. File input and output streams
Read Data
Data input
Write Data
Data output
Disk files Program
5. File stream classes
Class Contents
filebuf
To set the buffers to read and write.It contain
close() and open() as members.
fstreambase
Provides operations common to the file streams.
Serves as a base for fstream,ifstream and
ofstream class.
ifstream
Provides input operations. Inherites the functions
get(),getline(),read(),seekg() and tellg()
functions from istream.
ofstream
Provides output operations. Inherits put(),
seekp(),tellp() and write() functions from
ostream.
fstreams
Provides input and output operations. Inherits all
functions from istream and ostream classes
through iostream.
6. Suitable name for the file
Data type and structure
Purpose
Opening method
OPENING AND CLOSING A FILE
7. FILE CAN BE OPENED IN TWO WAYS:
FILE
CONSTRUCTOR
FUNCTION
MEMBER
FUNCTION OPEN()
8. A filename is used to initialize the file stream
object.
Create a file stream object to manage the
stream using the appropriate class.
The class ofstream is used to create the
output stream and the class ifstream to
create the input stream.
Initialize the file object with the desired file
name.
OPENING FILE USING CONSTRUCTOR
12. The function open() can be used to open
multiple files that use the same stream object.
SYNTAX:
OPENING FILE USING OPEN()
file-stream-class stream-object;
stream-object.open(“filename”);
14. fin2.open(“capital”);
for (int i=1; i<=10; i++)
{
if (fin1.eof() != 0)
{
cout << “exit from country n”;
exit(1);
}
fin1.getline(line, SIZE);
cout << “capital of” << line;
if (fin2.eof () !=0)
15. {
cout << “exit from capital n”;
exit (1);
}
fin2.getline (line,SIZE);
cout << line << “n”;
}
return 0;
}
16. The ifstream and ofstream constructors and the
function open() to create new files as well as to open
the existing files.
SYNTAX:
MORE ABOUT OPEN(): FILE MODES
stream-object.open(“filename”,mode);
17. The file mode parameter specifies the
purpose for which the file is opened.
The file mode parameter can take one of such
constants defined in the class ios.
ios::in for ifstream functions meaning open for
reading only.
ios::out for ofstream function meaning open for
writing only.
18. FILE MODE PARAMETERS
PARAMETER MEANING
ios::app Append to end-of-file
ios::ate Go to end-of-file on opening
ios::binary Binary file
ios::in Open file for reading only
ios::nocreate Open fails if the file does not exist
ios::noreplace Open fails if the file already exists
ios::out Open file for writing only
ios::trunc Delete the contents of the file if it exists