The document discusses C++ file input/output (I/O) streams and operations. It introduces the fstream, ifstream, and ofstream classes used to read from, write to, and read/write files. Examples show opening a file with the open() function, writing text to a file using ofstream, and formatting output using I/O manipulators. Common file operations like opening, writing, and closing are demonstrated.
1. I/O System Basics, File I/O UNIT 7
1 | P a g e A n a n d a K u m a r H N
I/O System Basics, File I/0: C++ stream classes, Formatted I/O, I/O manipulators,
fstream and the File classes, File operations
/* Normal C++ output*/
#include<iostream>
using namespace std;
int main()
{
cout<<"ATME"<<endl<<endl;
}
OUTPUT:
/*Formatted output*/
#include<iostream>
using namespace std;
int main()
{
cout.setf(ios::right);
cout.width(20);
cout<<"ATME"<<endl<<endl;
}
OUTPUT:
/* width( ) fill( ) setf( ) */
Example 1:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout.precision(7);
cout.fill('*'); //fill function
cout.width(19); // width function
cout.setf(ios::right);// setting flag
cout<<52.1234567<<endl;
cout<<setfill('?')<<setw(30);
cout<<"ATME"<<endl<<endl;
}
OUTPUT:
Example 2:
#include<iostream>
using namespace std;
int main()
{
cout.setf(ios::right);
cout.fill('$');
cout.width(20);
cout<<"ATME"<<endl<<endl;
}
OUTPUT:
/* showpos flag */
#include<iostream>
using namespace std;
int main()
{
int x=1000;
cout.setf(ios::showpos);
cout.fill('$');
cout.width(10);
cout<<"x="<<x<<endl<<endl;
}
OUTPUT:
/* showpoint flag */
#include<iostream>
using namespace std;
int main()
{
float x=1000;
cout.setf(ios::showpos);
cout.setf(ios::showpoint);
cout.fill('$');
cout.width(10);
cout<<"x="<<x<<endl<<endl;
}
OUTPUT:
2. I/O System Basics, File I/O UNIT 7
2 | P a g e A n a n d a K u m a r H N
/* Scientific flag */
#include<iostream>
using namespace std;
int main()
{
float x=1000.123;
cout.setf(ios::scientific);
cout.width(10);
cout<<"x="<<x<<endl<<endl;
}
OUTPUT:
#include<iostream>
using namespace std;
int main()
{
cout.precision(7);
cout.fill('*'); //fill function
cout.width(19); // width function
cout.setf(ios::left);// setting flag
cout.setf(ios::scientific);
cout<<52.1234567<<endl;
cout<<endl<<"unsetting flags"<<endl;
cout.unsetf(ios::left);
cout.unsetf(ios::scientific);
cout<<52.1234567<<endl;
}
OUTPUT:
/*C++ Manipulators */
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float x=188.9298;
cout<<hex<<100<<endl;
cout<<setfill('#')<<setw(20)<<x;
}
OUTPUT:
fstream and the File classes:
• 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.
These classes are derived directly or indirectly from
the classes istream, and ostream. We have already used
objects whose types were these classes: cin is an object
of class istream and cout is an object of class ostream.
Therfore, we have already been using classes that are
related to our file streams. And in fact, we can use our
filestreams the same way we are already used to use
cin and cout, with the only difference that we have to
associate these streams with physical files.
Let's see an example:
This code creates a file called example.txt and inserts a
sentence into it in the same way we are used to do with
cout, but using the file stream myfile instead.
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.n";
myfile.close();
return 0;
}
OUTPUT:
This program will create a txt file with filename as
example and it writes line "Writing this to a file.” And
closes the file.
Open a file:
The first operation generally performed on an object of
one of these classes is to associate it to a real file. This
procedure is known as to open a file. An open file is
represented within a program by a stream object
(an instantiation of one of these classes, in the previous
example this was myfile) and any input or output
operation performed on this stream object will be
applied to the physical file associated to it.
In order to open a file with a stream object we use its
member function open():
open (filename, mode);
Where filename is a null-terminated character
sequence of type const char * (the same type that
3. I/O System Basics, File I/O UNIT 7
2 | P a g e A n a n d a K u m a r H N
/* Scientific flag */
#include<iostream>
using namespace std;
int main()
{
float x=1000.123;
cout.setf(ios::scientific);
cout.width(10);
cout<<"x="<<x<<endl<<endl;
}
OUTPUT:
#include<iostream>
using namespace std;
int main()
{
cout.precision(7);
cout.fill('*'); //fill function
cout.width(19); // width function
cout.setf(ios::left);// setting flag
cout.setf(ios::scientific);
cout<<52.1234567<<endl;
cout<<endl<<"unsetting flags"<<endl;
cout.unsetf(ios::left);
cout.unsetf(ios::scientific);
cout<<52.1234567<<endl;
}
OUTPUT:
/*C++ Manipulators */
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float x=188.9298;
cout<<hex<<100<<endl;
cout<<setfill('#')<<setw(20)<<x;
}
OUTPUT:
fstream and the File classes:
• 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.
These classes are derived directly or indirectly from
the classes istream, and ostream. We have already used
objects whose types were these classes: cin is an object
of class istream and cout is an object of class ostream.
Therfore, we have already been using classes that are
related to our file streams. And in fact, we can use our
filestreams the same way we are already used to use
cin and cout, with the only difference that we have to
associate these streams with physical files.
Let's see an example:
This code creates a file called example.txt and inserts a
sentence into it in the same way we are used to do with
cout, but using the file stream myfile instead.
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.n";
myfile.close();
return 0;
}
OUTPUT:
This program will create a txt file with filename as
example and it writes line "Writing this to a file.” And
closes the file.
Open a file:
The first operation generally performed on an object of
one of these classes is to associate it to a real file. This
procedure is known as to open a file. An open file is
represented within a program by a stream object
(an instantiation of one of these classes, in the previous
example this was myfile) and any input or output
operation performed on this stream object will be
applied to the physical file associated to it.
In order to open a file with a stream object we use its
member function open():
open (filename, mode);
Where filename is a null-terminated character
sequence of type const char * (the same type that