Professional Documents
Culture Documents
Object Oriented Programming Using C++ CST-157 Unit-Iii Files Chapter-9
Object Oriented Programming Using C++ CST-157 Unit-Iii Files Chapter-9
using C++
CST-157
UNIT-III
Files
Chapter- 9
Hierarchy Diagram
Figure 8.2 I/O Classes
•2.fstreambase – this is the base class for fstream and, ifstream and
ofstream classes. therefore it provides the common function to
these classes. It also contains open() and close() functions.
• ios::out: It open file in output mode (i.e write mode) and place the
file pointer in beginning, if file already exist it will overwrite the
file.
• ios::in It open file in input mode(read mode) and permit reading
from the file.
• ios::binary Opens a file in binary mode.
14
University Institute of Engineering
PROGRAM 1
// This program demonstrates the declaration of an fstream
// object and the opening of a file. OUTPUT:
#include <iostream.h> Enter the name of a file you
wish to open
#include <fstream.h> or create: mystuff.dat [Enter]
void main(void) The file mystuff.dat was opened.
{
fstream dataFile; // Declare file stream object
char fileName[81];
cout << "Enter the name of a file you wish to open\n";
cout << "or create: ";
cin.getline(fileName, 81);
dataFile.open(fileName, ios::out);
cout << "The file " << fileName << " was opened.\n";
}
15
University Institute of Engineering
OPENING A FILE
DECLARATION
• fstream dataFile(“names.dat”, ios::in | ios::out);
void main(void)
{
fstream dataFile("names.dat", ios::in | ios::out);
cout << "The file names.dat was opened.\n";
}
Output:
The file names.dat was opened.
16
University Institute of Engineering
CLOSING A FILE
A File is closed by disconnecting it with the stream it is associated
with. The close( ) function is used to accomplish this task.
Syntax:
Stream_object.close( );
Example :
fout.close();
– Syntax
– Stream_object.eof( );
• Example :
• fout.eof( );
1. #include<fstream.h>
2. void main()
3. {
4. char s[80], ch;
5. ofstream file(“myfile.txt”); //open myfile.txt in default output
mode
6. do
7. { cout<<”\n enter line of text”;
8. gets(s); //standard input
9. file<<s; // write in a file myfile.txt
10. cout<<”\n more input y/n”;
11. cin>>ch;
12. }while(ch!=’n’||ch!=’N’);
13. file.close();
14. } //end of main
University Institute of Engineering
BINARY FILE
FUNCTIONS
read( )- read a block of binary data or reads a fixed number of bytes
from the specified stream and store in a buffer.
Syntax :
Stream_object.read((char *)& Object, sizeof(Object));
e.g:
file.read((char *)&s, sizeof(s));
write( ) – write a block of binary data or writes fixed number of bytes
from a specific memory location to the specified stream.
Syntax :
Stream_object.write((char *)& Object, sizeof(Object));
e.g:
file.write((char *)&s, sizeof(s));
University Institute of Engineering
FILE POINTER
• file.seekg(-p,ios::end), or
• file.seekg(p,ios::cur) i.e to move to p byte position from beginning,
end or current position.
• seekp(): It places the file pointer to the specified position in output
mode of file.
e.g file.seekp(p,ios::beg); or
file.seekp(-p,ios::end), or file.seekp(p,ios::cur)
i.e to move to p byte position from beginning, end or current
position.
• tellp(): This function returns the current working position of the file
pointer in the output mode.
e.g int p=file.tellp();
25
University Institute of Engineering
QUESTIONS FOR PRACTICE
1. What do you mean by buffer synchronization?
2. Name the member functions which are used to check the state of a
stream (all of them return a bool value). Define their purpose as
well.
3. What is formatted and unformatted input and output.