dimanche 28 avril 2019

Reading a text file with C++ to read in the value that follows a specific key word

I am attempting to write a generic text file reader in C++17 that will search a text file, line by line, for a specific key word and the code should read in the data point that follows that key word. I am writing it with a template function within a class so that it can read in any data type. In this example lets say that I have the following text file titled test.txt.

- test.txt file
integer key: 4
Float key: 6.04876
Double Key: 12.356554545476756565
String Key: test

The header file containing the template class looks like this. In this case the ReadTextFile class inherits another class to assist with checking the file status.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>

#ifndef read_files_hpp
#define read_files_hpp

class FileStatus
{
public:
    template <class container>
    bool file_exists(const container& file_name);
    template <class file_container>
    bool is_file_open(const file_container& file);
};
template <class container>
bool FileStatus::file_exists(const container& file_name)
    std::ifstream ifile(file_name.c_str());
    return (bool)ifile;
}

template <class file_container>
bool FileStatus::is_file_open(const file_container& file)
{
    return file.is_open();
}
// ----------------------------------------------------------------


class ReadTextFile : public FileStatus
{
public:
    template <class dtype, class container1, class container2>
    dtype read_key_words(const container1& file_name, const
                         container2& key_word);
};

template <class dtype, class container1, class container2>
dtype ReadTextFile::read_key_words(const container1& file_name,
                                   const container2& key_word)
{
    int j = 3;
    if (bool i = file_exists(file_name) != 1) {
        std::cout << "FATAL ERROR: " << file_name <<
        " not found" << std::endl;
        return -1;
    }
    std::ifstream file(file_name);
    if (is_file_open(file))
    {
        std::string line;
        while (std::getline(file, line))
        {
            std::cout << line.c_str() << std::endl;
        }
    }
    return j;
}
// ================================================================
// ================================================================
#endif

The calling program main.cpp looks like this;

#include <iostream>
#include <fstream>
#include "read_files.hpp"
int main() {
    std::string file_name("../data/unit_test/keys.txt");
    std::string key_word("integer key:");
    int j;
    j = txt_file.read_key_words<int>(file_name, key_word);
}

In this test case the class is being instantiated as type int so until the program is completely written I am returning the variable int j = 3; from the function read_key_words(). At present the code can read the file test.txt which is in the same directory and correctly read in each line. I would like the code to parse each line to recognize if the phrase integer key: is present and then read the variable that follows it, as the data type that the function is instantiated for, which in this case is an integer. How can I make this happen within the code?

Aucun commentaire:

Enregistrer un commentaire