Topic:: Files & I/O
Topic:: Files & I/O
Topic:: Files & I/O
1
MODULE -5
OUTLINE
Basic Input / Output in C++
Input Stream
Output Stream
2
MODULE -5
3
MODULE -5
Module 4 Session 4 7
Header files available in C++ for Input
– Output operation are
iostream:
iostream stands for standard input output stream.
This header file contains definitions to objects like cin, cout,
cerr etc.
iomanip:
iomanip stands for input output manipulators.
• The methods declared in this files are used for
manipulating streams.
• This file contains definitions of setw, setprecision etc.
fstream:
This header file mainly describes the file stream.
• This header file is used to handle the data being read from
a file as input or data being written into the file as output.
the objects defined in the header
file iostream
Standard output stream (cout)
standard input stream (cin)
Un-buffered standard error stream (cerr)
buffered standard error stream (clog)
Module 4 Session 4 9
#include <iostream>
int main( ) {
char sample[] = “ I Love Linuxc++";
cout << sample << " - A computer science portal for learners";
return 0;
}
int main()
{
int age;
cout << "Enter your age:";
cin >> age;
cout << "\nYour age is: "<<age;
return 0;
}
Module 4 Session 4 10
The ostream Class
• The formatted output functions
(via overloaded stream insertion operator <<)
convert numeric values (such as int, double)
from their internal representations
• unformatted output functions
(e.g., put(), write()) outputs the bytes as they
are, without format conversion.
Module 4 Session 4 11
MODULE -5
12
MODULE -5
13
MODULE -5
14
MODULE -5
15
MODULE -5
iomanip
• iomanip stands for input output manipulators.
16
MODULE -5
CLASS TEMPLATES
cout << "|" << setw(5) << 123 << "|" << 123 << endl;
17
MODULE -5
File Input/Output
19
File Output
The steps are:
File Modes
File modes are defined as static public member in ios_base superclass.
They can be referenced from ios_base or its subclasses –
we typically use subclass ios.
21
File Input
The steps are:
Construct an istream object.
Connect it to a file (i.e., file open) and set the mode of
file operation.
Perform output operation via extraction << operator
or read(), get(), getline() functions.
Disconnect (close the file) and free the istream object.
#include <fstream> .......
fstream fin;
fin.open(filename, mode);
...... fin.close();
22
// writing on a text file [file example.txt]
#include <iostream>
#include <fstream> This is a line.
using namespace std;
int main () This is another line.
{ ofstream myfile ("example.txt");
if (myfile.is_open())
{ myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
23
// reading a text file This is a line. This
#include <iostream> is another line.
#include <fstream>
#include <string>
using namespace std;
int main ()
{ string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
Else
cout << "Unable to open file"; return 0;
}
24
Random Access File
Random access file is associated with a file pointer, which can be moved directly to any
location in the file.
You can position the input pointer via seekg() and output pointer via seekp().
Each of them has two versions: absolute and relative positioning.
istream & seekg (streampos pos); // absolute position relative to beginning
25
offset counted from the beginning of
ios::beg
the stream
ios::cur offset counted from the current position
offset counted from the end of the
ios::end
stream
26
/ obtaining file size size is: 40
#include <iostream> bytes.
#include <fstream>
using namespace std;
int main ()
{
streampos begin,end;
ifstream myfile ("example.bin", ios::binary);
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size is: " << (end-begin) << " bytes.\n";
return 0;
}
27
Set fill character
Sets c as the stream's fill character.
// setfill example
#include <iostream> // std::cout, std::endl
#include <iomanip> // std::setfill, std::setw
int main ()
{ std::cout << std::setfill ('x') << std::setw (10);
std::cout << 77 << std::endl; return 0;
}
Output:
xxxxxxxx77
28
Set decimal precision
Sets the decimal precision to be used to format floating-point values on
output operations.
// setprecision example
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
int main ()
{ double f =3.14159;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n'; return 0;
}
Output:
3.1416
3.14159
3.14159
3.141590000
29
setw
Set field width
77
Example
30