Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. - test.txt file
  2. integer key: 4
  3. Float key: 6.04876
  4. Double Key: 12.356554545476756565
  5. String Key: test
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <string>
  10. #include <vector>
  11. #include <sstream>
  12.  
  13. #ifndef read_files_hpp
  14. #define read_files_hpp
  15.  
  16. class FileStatus
  17. {
  18. public:
  19. template <class container>
  20. bool file_exists(const container& file_name);
  21. template <class file_container>
  22. bool is_file_open(const file_container& file);
  23. };
  24. template <class container>
  25. bool FileStatus::file_exists(const container& file_name)
  26. std::ifstream ifile(file_name.c_str());
  27. return (bool)ifile;
  28. }
  29.  
  30. template <class file_container>
  31. bool FileStatus::is_file_open(const file_container& file)
  32. {
  33. return file.is_open();
  34. }
  35. // ----------------------------------------------------------------
  36.  
  37.  
  38. class ReadTextFile : public FileStatus
  39. {
  40. public:
  41. template <class dtype, class container1, class container2>
  42. dtype read_key_words(const container1& file_name, const
  43. container2& key_word);
  44. };
  45.  
  46. template <class dtype, class container1, class container2>
  47. dtype ReadTextFile::read_key_words(const container1& file_name,
  48. const container2& key_word)
  49. {
  50. int j = 3;
  51. if (bool i = file_exists(file_name) != 1) {
  52. std::cout << "FATAL ERROR: " << file_name <<
  53. " not found" << std::endl;
  54. return -1;
  55. }
  56. std::ifstream file(file_name);
  57. if (is_file_open(file))
  58. {
  59. std::string line;
  60. while (std::getline(file, line))
  61. {
  62. std::cout << line.c_str() << std::endl;
  63. }
  64. }
  65. return j;
  66. }
  67. // ================================================================
  68. // ================================================================
  69. #endif
  70.  
  71. #include <iostream>
  72. #include <fstream>
  73. #include "read_files.hpp"
  74. int main() {
  75. std::string file_name("../data/unit_test/keys.txt");
  76. std::string key_word("integer key:");
  77. int j;
  78. j = txt_file.read_key_words<int>(file_name, key_word);
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement