Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6382109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:28:48+00:00 2026-05-25T02:28:48+00:00

I want to read and parse a text file in C++ in a generic

  • 0

I want to read and parse a text file in C++ in a generic way. The file is always made of key-value pairs, one per line. The key is templated as well as the value. I foresee the key and the values to always be a basic type (int, float, string).

My problem is that I don’t know how to create a transform the key or value string into the correct type.

I tried the following :

template<class Key, class T> inline
void EventReportReader<Key, T>::validateFileFormat()
{
    // Read file line by line and check that the first token is of type Key and the second one of type T

    std::string line;
    try {
        boost::regex re( "(\\S+)\\s+(.*)" );
        while( getline( inStream_, line ) ) {
            boost::cmatch matches;
            if( boost::regex_match( line.c_str(), matches, re ) ) {
                std::cout << re << " matches " << line << std::endl;
                std::cout << "   1st : " << matches[1] << "\n   2nd : " << matches[2] << std::endl;
                // test types
                Key *k = dynamic_cast<Key*>(&matches[1]);
                T t = dynamic_cast<T>(matches[2]);
            }
        }
    }
    catch( boost::regex_error& e ) {
        // todo problem with regular expression, abort
    }
}

And the use of this method is as follow :

// This in turn calls the method validateFileFormat
EventReportReader<float, int> reader( testFileName );

The result is

/home/vonhalle/dev/EventBasedReport/libs/event_based_report/EventReportReader.h:121:60: error: cannot dynamic_cast ‘(const boost::sub_match*)matches.boost::match_results::operator[] with BidiIterator = const char*, Allocator = std::allocator >, boost::match_results::const_reference = const boost::sub_match&’ (of type ‘const struct boost::sub_match’) to type ‘float’ (target is not pointer or reference to class)
/home/vonhalle/dev/EventBasedReport/libs/event_based_report/EventReportReader.h:122:53: error: cannot dynamic_cast ‘matches.boost::match_results::operator[] with BidiIterator = const char*, Allocator = std::allocator >, boost::match_results::const_reference = const boost::sub_match&’ (of type ‘const struct boost::sub_match’) to type ‘int’ (target is not pointer or reference)

How should I do it ?
Is it even possible ?

EDIT:
The file might look like this if the template is < float, int >

1.14 5
2.34 78
0.56 24

or this if the template is < int, string >

23 asdf
45 2222
1 bbbb

EDIT2:

The problem statement above is partially wrong. The key is never a string, the value can be a string. Therefore, whatever is before the first space is the key and the rest is the value. Sorry about this mistake.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:28:49+00:00Added an answer on May 25, 2026 at 2:28 am

    I think your basic approach is wrong.
    You seem to be trying to use template-meta programming to achieve your goals.
    This is probably not a good idea.

    A simpler approach is just to use C++ streams.
    These stream objects already know how to read all the basic types. And anybody that want to do anything in C++ will add the appropriate input and output operators to stream their class; so it is pretty universal that you will be able to read any type as both key and value (with the restriction that it must fit on one line).

    So now you just need to use standard template logic to define an operator that will read two objects of different types on a single line.

    Try this:

    #include <string>
    #include <memory>
    #include <fstream>
    #include <sstream>
    #include <vector>
    #include <iterator>
    #include <algorithm>
    
    // These can be any types.    
    typedef std::string   Key;
    typedef int           Value;
    
    // The data type to hold the data.
    template<typename K,typename V>
    class Data: public std::pair<K, V>
    {
    
    };
    

    Here is the code that will read one record from one line of the file:
    Note that the data type Data and this input operator are both templated and can thus ready Key/Value pairs of any objects (as long as those objects know how to stream themselves).

    template<typename K,typename V>
    std::istream& operator>>(std::istream& stream, Data<K,V>& data)
    {
        // Read a line into a local string.
        std::string  line;
        std::getline(stream,line);
    
        // convert the line into a stream and read the key/value from the line
        std::stringstream  linestream(line);
        linestream >> data.first >> data.second;
    
    
        // If the linestream is bad, then reading the key/value failed
        // If reading one more `char` from the linestream works then there is extra crap in the line
        // thus we have bad data on a line.
        //
        // In either case set the bad bit for the input stream.
        char c;
        if ((!linestream) || (linestream >> c))
        {
            stream.setstate(std::ios::badbit);
        }
        // return the stream.
        return stream;
    }
    

    Now using it simply means using a stream:

    int main()
    {
        // The input file
        std::ifstream      file("Plop");
    
        // We will convert the file and store it into this vector.
        std::vector<Data<Key,Value> >  data;
    
        // Now just copy the data from the stream into the vector.
        std::copy(std::istream_iterator<Data<Key,Value> >(file),
                std::istream_iterator<Data<Key, Value> >(),
                std::back_inserter(data)
                );
    }
    

    Note: In the above example a key must be a single word (as it is read using a string). If you want a key as a string that contains a space you need to do some extra work. But that is the subject of another question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read each line from a text file and store them in
I want to parse the text line from the Wavefront OBJ file . Currently
I want to read words in a text file of a line separated by
I have a text file. I want read that file. But In that if
I want to read line n1->n2 from file foo.c into the current buffer. I
Whats wrong with my code.. I want it to read a text file like
I am exporting UTF-8 text from Excel and I want to read and parse
I want to read graph adjacency information from a text file and store it
I want to read an specific xml node and its value for example <customers>
I want to read an xml file, apply a transform, then write to another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.