IOStream Library

Download Report

Transcript IOStream Library

Unit - 10
IOStream Library
Input / Output in C++
Unit Introduction
This unit covers different features in standard
IOStream Library
2
Unit Objectives
After covering this unit you will understand…
 IOStream library
 File operations
 Buffering operations
 Seeking operations
 Output stream formatting
 IOStream manipulators
 Creating manipulators
3
IOStream Introduction


Input and Output are the basic programming
functions
Without IOStream Library:
requires hundreds of lines of code for usage and
manipulation
 are error prone with dangling pointers, uninitialized
pointers etc

4
IOStream Library

IOStream Library helps in:
ease of coding
 error handling
 safer and efficient results


Deals with the following I/O functions in a safe,
efficient and easier manner:
Standard Input
 Standard Output
 Files
 Memory Blocks

5
IOStreams Features





Provides an easier interface
Handles menial tasks like closing the file pointer,
when the file is not in use etc
Error handling is automatic when I/O error
occurs
Constructor handles all the initialisation
Destructor handles all the cleanup tasks like
removing dangling pointers, memory cleanup etc
6
File IOStreams

File IOStreams are simple to use:
Declare an Object
 Use the file I/O functions
 Destroy the object (the destructor is called
automatically when the object goes out of scope)

7
Example: File IOStreams
#include <fstream.h>
#include <iostream.h>
void main()
{
const int sz = 100; // Buffer size;
char buf[sz];
{
ifstream in(”myfile.cpp");
// Read
ofstream out(”myfile.out");
// Write
int i = 1;
// Line counter
while(in.get(buf, sz))
{
// Line input
in.get();
// get() does not read ‘\n’
cout << buf << endl;
// use endl for ‘\n’
// File output just like standard I/O:
out << i++ << ": " << buf << endl;
8
Example: File IOStreams (contd.)
}
}
// Destructors close in & out
ifstream in(”myfile.out");
// More convenient line input:
while(in.getline(buf, sz))
{
char* cp = buf;
while(*cp != ':')
{
cp++;
}
cp += 2;
// Past ": "
cout << cp << endl;
}
}
9
File Open Modes



You can control the way a file is opened by
changing a default argument
These flags can be combined using a bitwise OR
The different modes are:
ios::in
 ios::out
 ios::app
 ios::nocreate

10
File Open Modes (contd.)
ios::noreplace
 ios::trunc
 ios::binary

11
Buffering in IOStreams


You can buffer all the bytes in a file using
buffering
It provides an easy and efficient solution to
reading files or other I/O features
12
Example: Buffering IOStreams
#include <fstream.h>
#include <iostream.h>
void main()
{
ifstream in(“myfile.cpp”);
cout << in.rdbuf();
// outputs entire file
// another way to output the file
while (in.get(*cout.rdbuf()))
// redirects it to cout
{
in.ignore();
// ignore the terminator ‘\n’
}
}
13
Seeking in IOStream


You can move the stream pointer to different
positions and read or write bytes
There are three seek functions:
ios::beg From beginning of stream
 ios::cur Current position in stream
 ios::end From end of stream

14
Example: Seeking in IOStream
#include <iostream>
#include <fstream>
void main()
{
ifstream in("Iofile.cpp");
ofstream out("Iofile.out");
out << in.rdbuf();
in.close();
out.close();
// Copy file
// Open for reading and writing:
ifstream in2("Iofile.out", ios::in | ios::out);
ostream out2(in2.rdbuf());
cout << in2.rdbuf();
// Print whole file
out2 << "Where does this end up?";
out2.seekp(0, ios::beg);
// put pointer
15
Example: Seeking in IOStream (contd.)
out2 << "And what about this?";
in2.seekg(0, ios::beg);
cout << in2.rdbuf();
// get pointer
}
16
Output Stream Formatting


You can format the output stream in STL, just
like you can using printf in C++
The different formatting functions are:

Internal formatting data
ios::skipws
 ios::showbase
 ios::showpoint
 ios::uppercase
 ios::showpos
 ios::unitbuf

17
Output Stream Formatting (contd.)


ios::stdio
Format fields

ios::basefield




ios::floatfield




ios::dec
ios::hex
ios::oct
ios::scientific
ios::fixed
automatic
ios::adjustfield


ios::left
ios::right
18
Output Stream Formatting (contd.)


ios::internal
Width, fill and precision
int ios::width( )
 int ios::width(int n)
 int ios::fill( )
 int ios::fill(int n)
 int ios::precision( )
 int ios::precision(int n)

19
Iostream Manipulators



Output stream formatting can be a tedious task
To make things easier to read and write, a set of
manipulators are provided
The manipulators are:
showbase / noshowbase
 showpos / noshowpos
 uppercase / nouppercase
 showpoint / noshowpoint

20
IOStream Manipulators (contd.)
skipws / noskipws
 left / right / internal
 scientific / fixed



Some manipulators can have arguments, which
require <iomanip.h> header
The argument supplied manipulators are:
setiosflags (fmtflags n)
 resetiosflags(fmtflags n)

21
IOStream Manipulators (contd.)
setbase(base n)
 setfill(char n)
 setprecision(int n)
 setw(int n)

22
Example: IOStream Manipulators
#include <fstream>
#include <iomanip>
void main()
{
ofstream trc("trace.out");
int i = 47;
float f = 2300114.414159;
char* s = "Is there any more?";
trc << setiosflags(ios::unitbuf | ios::showbase |
ios::uppercase | ios::showpos);
trc << i << endl;
// Default to dec
trc << hex << i << endl;
trc << resetiosflags(ios::uppercase) << oct << i;
trc.setf(ios::left, ios::adjustfield);
trc << resetiosflags(ios::showbase) << dec
<< setfill('0');
trc << "fill char: " << trc.fill() << endl;
trc << setw(10) << i << endl;
23
Example: IOStream Manipulators (contd.)
trc << resetiosflags(ios::showpos)
<< setiosflags(ios::showpoint) << "prec = "
<< trc.precision() << endl;
trc.setf(ios::scientific, ios::floatfield);
trc << f << endl;
trc.setf(ios::fixed, ios::floatfield);
trc << f << endl;
trc << setprecision(20);
trc << "prec = " << trc.precision() << endl;
trc << resetiosflags( ios::showpoint | ios::unitbuf);
}
24
Creating Manipulators



You can create your own manipulators
The manipulator may or may not have
arguments
The declaration for endl is
ostream& endl(ostream&);
cout << “howdy” << endl;


the endl produces the address of that function
Applicator calls the function, passing it the
ostream object as an argument
25
Example: Creating Manipulators
#include <iostream>
ostream& nl(ostream& os)
{
return os << '\n';
}
void main()
{
cout << "newlines" << nl << "between" << nl
<< "each" << nl << "word" << nl;
}
/*
The expression
os << '\n';
calls a function that returns os, which is what is returned
from nl
*/
26
Unit Summary
In this unit you have covered …
 Overview of the IOStream library
 Different features of IOStream
 IOStream operations & manipulators
 Creating custom manipulators
27