Click here to Skip to main content
Click here to Skip to main content

Tagged as

C++ & STL: Reading a text file into a collection with little code

, 12 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use istream_iterator and the copy algorithm to populate an STL collection from a text file with very little code
There is a simple function I wrote to populate an STL list with the elements in a text file. The two parameters are the filename and the list to populate. This is a template function, so it will support lists of any type of element. If you want to use a different STL collection, you can change the code from using a list to your preferred collection type. I wrote this function to clear the collection before reading the file.
 
The function requires the following headers and uses the following:
#include <algorithm>
#include <iostream>
#include <fstream>
#include <iterator>
using std::ifstream;
using std::istringstream;
using std::list;
using std::for_each;
using std::istream_iterator;
using std::back_insert_iterator;
 
Following is the actual function. The remarkable thing about the STL is its power, and this is one example: With istream_iterator and the std::copy() function, reading the file & populating the collection can be done with very little code (practically one line).
template <class T>
void ReadDataFile(const char* pFilename, list<T>& pDataList)
{
    pDataList.clear();
    ifstream inFile(pFilename, std::ios::in);
    if (inFile.good())
    {
        std::copy(istream_iterator<T>(inFile), istream_iterator<T>(), back_insert_iterator<list<T> >(pDataList));
        inFile.close();
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

CalicoSkies

United States United States
I was born and raised (and currently still reside in) northwest Oregon, USA. I started using computers at a very young age, maybe 4 or 5 years old, using my dad's computers. I got my own computer when I was 12 (1992). Later, I realized that I really enjoy programming, and in college, I decided to get a degree in software engineering. In 2005, I earned my bachelor's degree in software engineering from Oregon Institute of Technology.
 
I have been working as a software engineer since October 2003. So far, I've had experience with C++, PHP, JavaScript, Perl, Bash shell scripting, C#, and HTML. I have developed GUIs with MFC, wxWidgets, and WinForms. I also have experience with open-source technologies such as Linux, Apache, MySQL, and PostgreSQL. My favorite Linux text editor is Vim.
 
In my personal time I enjoy tinkering with computers (building PCs, gaming, occasionally working on personal programming projects), playing music (I enjoy playing guitar and synthesizer/piano), and spending time with family & friends. I also enjoy reading the occasional book (pretty much anything with a good story, but usually sci-fi, drama, programming books, advice books, and scientific non-fiction, such as books written by physicist Michio Kaku). Currently, my favorite TV show is The Office; I also like watching the news when I can. I also enjoy the occasional movie (sci-fi, comedy, action, drama; pretty much anything with a good story).

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 12 Apr 2011
Article Copyright 2011 by CalicoSkies
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid