Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - test.txt file
- integer key: 4
- Float key: 6.04876
- Double Key: 12.356554545476756565
- String Key: test
- #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
- #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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement