C++
  Home arrow C++ arrow Page 3 - Advanced File Handling with Streams in C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Advanced File Handling with Streams in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2009-04-14

    Table of Contents:
  • Advanced File Handling with Streams in C++
  • Printing from Containers
  • Reading Inside a Container
  • Writing to Two Streams

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Advanced File Handling with Streams in C++ - Reading Inside a Container


    (Page 3 of 4 )

    You can use the same trick in the other direction (input), since it's just as easy. Let us suppose you have the input waiting for input in a predefined way. For example, we will try with a sequence of int types. Now we will copy in the other direction, so we need to indicate from where to copy, and to where. The first two members are the istream_iterator that will delimit the borders. When we pass a class to it, it will return an iterator to its start, and no parameter will just point to the end of file.

    The last argument is a little more interesting. Under normal conditions, whenever you make a copy, you make sure that the target is large enough to hold the incoming stream of data. This is achieved by calling the destination resize() function, but in the case of this iterator type, this process is simplified by calling the push_back() method on the destination iterator. Hence, a vector can handle any size as long as you have enough physical and virtual memory available.


    #include <iostream>

    #include <vector>

    #include <algorithm>

    #include <fstream>

    using namespace std;


    int main()

    {

    vector<int> numbers;

     

    ifstream in;

    in.open("In.txt");

    if (!in)

    {

    return 0; // terminate program, nothing more to do

    }


    copy( istream_iterator<int>(in), istream_iterator<int>(), back_insert_iterator<vector<int>>(numbers));


    copy(numbers.begin(), numbers.end(), ostream_iterator<char>(cout, "...n"));

     

    return 1;

    }


    The input file contained the following:


    97 98 122 100 101A 12


    With the output:


    a...

    b...

    z...

    d...

    e...

    Press any key to continue . . .


    The rule stands. If you have defined a special class, and if you define the extraction function (operator >>) for it, you will be able to call it for this kind of input. For each member, the extraction function will be called to resolve the input. Naturally, you need to call the iterator creations to those kinds of template arguments.

    More C++ Articles
    More By Gabor Bernat


     

    C++ ARTICLES

    - 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++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek