Recipe 10.3. Writing Your Own Stream Manipulators
Problem
You need a stream manipulator that does something the standard ones can't. Or, you want to have a
single manipulator set several flags on the stream instead of calling a set of manipulators each time you
want a particular format.
Solution
To write a manipulator that doesn't take an argument (à la left), write a function that takes an ios_base
parameter and sets stream flags on it. If you need a manipulator that takes an argument, see the
discussion a little later. Example 10-4
shows how to write a manipulator that doesn't take an argument.
Example 10-4. A simple stream manipulator
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// make floating-point output look normal
inline ios_base& floatnormal(ios_base& io) {
io.setf(0, ios_base::floatfield);
return(io);
}
int main( ) {
ios_base::fmtflags flags =  // Save old flags
cout.flags( );
double pi = 22.0/7.0;
cout << "pi = " << scientific   // Scientific mode
<< pi * 1000 << '\n';
cout << "pi = " << floatnormal
<< pi << '\n';
cout.flags(flags);
}
Discussion
There are two kinds of manipulators: those that accept arguments and those that don't. Manipulators that
take no arguments are easy to write. All you have to do is write a function that accepts a stream
parameter, does something to it (sets a flag or changes a setting), and returns it. Writing a manipulator
that takes one or more arguments is more complicated because you need to create additional classes and
functions that operate behind the scenes. Since argument-less manipulators are simple, let's start with
those.
After reading Recipe 10.1
, you may have realized that there are three floating-point formats and only
two manipulators for choosing the format. The default format doesn't have a manipulator; you have to set
a flag on the stream to get back to the default format, like this:
myiostr.setf(0, ios_base::floatfield);
But for consistency and convenience, you may want to add your own manipulator that does the same
thing. That's what Example 10-4
does. The floatnormal manipulator sets the appropriate stream flag to
Page 381
ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html
Pdf will no pages selected - Split, seperate PDF into multiple files in C#.net, ASP.NET, MVC, Ajax, WinForms, WPF
Explain How to Split PDF Document in Visual C#.NET Application
pdf no pages selected; pdf splitter
Pdf will no pages selected - VB.NET PDF File Split Library: Split, seperate PDF into multiple files in vb.net, ASP.NET, MVC, Ajax, WinForms, WPF
VB.NET PDF Document Splitter Control to Disassemble PDF Document
pdf rotate single page; cannot select text in pdf
Page 382
ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html
VB.NET TWAIN: TWAIN Image Scanning in Console Application
First, there is no SelectSourceDialog in VB.NET TWAIN console Here we will illustrate the benefits of this VB.NET how to scan multiple pages to one PDF or TIFF
break a pdf into multiple files; break up pdf into individual pages
VB.NET PowerPoint: Convert & Render PPT into PDF Document
on and VB.NET PDF editing add-on will be used. As our VB.NET PowerPoint to PDF conversion add-on and edit .pptx document file independently, no other external
break pdf password; break apart pdf
Page 383
ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html
C# Image: Create C#.NET Windows Document Image Viewer | Online
C# Windows Document Image Viewer Features. No need for viewing multiple document & image formats (PDF, MS Word The following list will give you a broad overview
break apart pdf pages; a pdf page cut
VB.NET Word: Use VB.NET Code to Convert Word Document to TIFF
VB.NET Word to TIFF image converting application, no external Word free to contact us and we will offer you more user guides with RasteEdge .NET PDF SDK using
break pdf password online; break a pdf into separate pages
Recipe 10.4. Making a Class Writable to a Stream
Problem
You have to write a class to an output stream, either for human readability or persistent storage, i.e.,
serialization.
Solution
Overload operator<< to write the appropriate data members to the stream. Example 10-6
shows how.
Example 10-6. Writing objects to a stream
#include <iostream>
#include <string>
using namespace std;
class Employer {
friend ostream& operator<<              // This has to be a friend 
(ostream& out, const Employer& empr); // so it can access non-
public:                                    // public members
Employer( ) {}
~Employer( ) {}
void setName(const string& name) {name_ = name;}
private:
string name_;
};
class Employee {
friend ostream& operator<<
(ostream& out, const Employee& obj); 
public:
Employee( ) : empr_(NULL) {}
~Employee( ) {if (empr_) delete empr_;}
void setFirstName(const string& name) {firstName_ = name;}
void setLastName(const string& name) {lastName_ = name;}
void setEmployer(Employer& empr) {empr_ = &empr;}
const Employer* getEmployer( ) const {return(empr_);}
private:
string firstName_;
string lastName_;
Employer* empr_;
};
// Allow us to send Employer objects to an ostream...
ostream& operator<<(ostream& out, const Employer& empr) {
out << empr.name_ << endl;
return(out);
}
// Allow us to send Employee objects to an ostream...
ostream& operator<<(ostream& out, const Employee& emp) {
out << emp.firstName_ << endl;
out << emp.lastName_ << endl;
if (emp.empr_)
out << *emp.empr_ << endl;
Page 384
ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html
C# HTML5 PDF Viewer SDK deployment on IIS in .NET
This page will navigate users how to deploy HTML5 PDF to the place where you store XDoc.PDF.HTML5 Viewer The site configured in IIS has no sufficient authority
acrobat split pdf bookmark; break apart a pdf in reader
VB.NET PDF - VB.NET HTML5 PDF Viewer Deployment on IIS
This page will navigate users how to deploy HTML5 PDF to the place where you store XDoc.PDF.HTML5 Viewer The site configured in IIS has no sufficient authority
split pdf files; pdf split
Page 385
ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html
VB.NET TIFF: .NET TIFF Printer Control; Print TIFF Using VB.NET
TIFF document printing add-on has no limitation on VB.NET TIFF printing API will automatically send powerful & profession imaging controls, PDF document, image
add page break to pdf; break apart a pdf
C# Word: C#.NET Word Rotator, How to Rotate and Reorient Word Page
Remarkably, no other external products, including Microsoft page rotation control SDK will integrate the & profession imaging controls, PDF document, image to
acrobat separate pdf pages; combine pages of pdf documents into one
Page 386
ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html
Recipe 10.5. Making a Class Readable from a Stream
Problem
You have written an object of some class to a stream, and now you need to read that data from the
stream and use it to initialize an object of the same class.
Solution
Use operator>> to read data from the stream into your class to populate its data members, which is
simply the reverse of what Example 10-6
does. See Example 10-7
for an implementation.
Example 10-7. Reading data into an object from a stream
#include <iostream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;
class Employee {
friend ostream& operator<<              // These have to be friends
(ostream& out, const Employee& emp); // so they can access
friend istream& operator>>              // nonpublic members
(istream& in, Employee& emp);
public:
Employee( ) {}
~Employee( ) {}
void setFirstName(const string& name) {firstName_ = name;}
void setLastName(const string& name) {lastName_ = name;}
private:
string firstName_;
string lastName_;
};
// Send an Employee object to an ostream...
ostream& operator<<(ostream& out, const Employee& emp) {
out << emp.firstName_ << endl;
out << emp.lastName_ << endl;
return(out);
}
// Read an Employee object from a stream
istream& operator>>(istream& in, Employee& emp) {
in >> emp.firstName_;
in >> emp.lastName_;
return(in);
}
int main( ) {
Employee emp;
string first = "William";
string last = "Shatner";
Page 387
ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html
Page 388
ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html
Page 389
ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html
Recipe 10.6. Getting Information About a File
Problem
You want information about a file, such as its size, device, last modification time, etc.
Solution
Use the C system call stat in <sys/stat.h>. See Example 10-8
for a typical use of stat that prints out a
few file attributes.
Example 10-8. Obtaining file information
#include <iostream>
#include <ctime>
#include <sys/types.h>
#include <sys/stat.h>
#include <cerrno>
#include <cstring>
int main(int argc, char** argv )
{
struct stat fileInfo;
if (argc < 2) {
std::cout << "Usage: fileinfo <file name>\n";
return(EXIT_FAILURE);
}
if (stat(argv[1], &fileInfo) != 0) {  // Use stat( ) to get the info
std::cerr << "Error: " << strerror(errno) << '\n';
return(EXIT_FAILURE);
}
std::cout << "Type:         : ";
if ((fileInfo.st_mode & S_IFMT) == S_IFDIR) { // From sys/types.h
std::cout << "Directory\n";
} else {
std::cout << "File\n";
}
std::cout << "Size          : " <<
fileInfo.st_size << '\n';               // Size in bytes
std::cout << "Device        : " <<
(char)(fileInfo.st_dev + 'A') << '\n';  // Device number
std::cout << "Created       : " <<
std::ctime(&fileInfo.st_ctime);         // Creation time
std::cout << "Modified      : " <<
std::ctime(&fileInfo.st_mtime);         // Last mod time
}
Discussion
The C++ standard library supports manipulation of file content with streams, but it has no built-in
support for reading or altering the metadata the OS maintains about a file, such as its size, ownership,
permissions, various timestamps, and other information. However, standard C contains a number of
standard system call libraries that you can use to get this kind of information about a file, and that's what 
Example 10-8
uses.
There are two parts to obtaining file information. First, there is a struct named stat that contains members
that hold data about a file, and second there is a system call (function) of the same name, which gets
Page 390
ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html
Documents you may be interested
Documents you may be interested