Home arrow C++ arrow Page 4 - Advanced File Handling with Streams in C++
C++

Advanced File Handling with Streams in C++


In the previous article of this series, titled "File Handling and Streams in C++," you saw how to handle files in an easy and convenient way. Getting input data from a file or putting such data into a file should be familiar to you now. In this article, we'll take a look at some more advanced tasks and tricks so you can gain speed and efficiency in your work.

Author Info:
By: Gabor Bernat
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
April 14, 2009
TABLE OF CONTENTS:
  1. · Advanced File Handling with Streams in C++
  2. · Printing from Containers
  3. · Reading Inside a Container
  4. · Writing to Two Streams

print this article
SEARCH DEVARTICLES

Advanced File Handling with Streams in C++ - Writing to Two Streams
(Page 4 of 4 )

Often, you may need to stream your data into two places at the same time. A perfect situation similar to this is when you need to dump the screen's content to an output file. Therefore, whatever will be printed to the screen will be contained by a file as well. To resolve this, you always need to create another stream, and later call an insertion process for both the console and the file.

This is counterproductive. Besides, you might forget one step and then struggle due to a strange inconsistency. The perfect solution is to create a stream type to which we can bind both of them. Deriving from the ostream class is not a good idea, as this offers no virtual over-writable functions.

Doing the same thing at the streambuffer class is possible, as I have described in my article called "Extending the Basic Streams in C++". Feel free to search for it here on the network if you are interested in learning about this approach in more detail about this approach. However, there is a much simpler way to do it.

#include <iostream>

#include <fstream>

#include <iomanip>

using namespace std;


template<typename charType,

typename twinCharTraits = std::char_traits<charType> >


struct basic_TwinStream : basic_ostream<charType,twinCharTraits>

{

//define our new basic type

typedef basic_ostream<charType,twinCharTraits> TwinType;

 

// constructor -> we initialize the two stream references

// construct this by using as buffer for the stream the // buffer of the first

basic_TwinStream(std::ostream& os_1, std::ostream& os_2)

: TwinType(os_1.rdbuf()), m_os_1(os_1), m_os_2(os_2)

{ }


// declare the insertion operator, this will be called on // both of them

template<typename T>

basic_TwinStream& operator<<(const T& t)

{

m_os_1 << t;

m_os_2 << t;

return *this;

}


// a insertion operator for the manipulators

basic_TwinStream& operator<<

(TwinType& (__cdecl *manip)(TwinType& ))

{

m_os_1 << manip;

m_os_2 << manip;

return *this;

}


private:

std::ostream& m_os_1;

std::ostream& m_os_2;

};


typedef basic_TwinStream<char> TwinStream;

typedef basic_TwinStream<wchar_t > WTwinStream;


int main()

{


ofstream out("OutIn.txt");

TwinStream oneTwinStream(cout, out);

oneTwinStream <<left << setw(25) << setfill('@')

<< "Yeti is from north" << endl;

}


The key observation here was to detect that, for a stream, the most-used function is the insertion operator. If we can create a class where we simply overwrite, we are close to a perfect situation. However, another detail must be considered. What if we want to use the manipulators to format our output? Implementing in this fashion is necessary, as we will lack any other functions.

Before we venture further, let us see the result. Here are the outputs first, straight from the console and followed by the output into the OutIn text file:


Yeti is from north@@@@@@@

Press any key to continue . . .


Yeti is from north@@@@@@@



As you can see, we will be losing the side of available functions to call. In fact, all that we can do is call the insertion operator on both types and manipulators. Calling more advanced functions, like the unsetf() or setf(), is on the list of things to be implemented, and I will leave this task to the reader. The fact that the other functions are missing is a good thing, as the user cannot use some of them that make no sense in this context.

As we are close in to the end of this article, I must congratulate you for reading it all the way through,  and tell you to keep up the good work, because eventually it will pay off; you will see that. If you have further questions related to this article, do not hesitate to post them on the blog, or even better, join the DevHardware Forums and present your questions/ideas to a whole community. Finally, remember to "Live with Passion!"


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials