Advertisement
Guest User

Files.cpp

a guest
Apr 19th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include "Files.h"
  2.  
  3.  
  4. namespace ulib
  5. {
  6.     namespace files {
  7.  
  8.         // ReadFile
  9.         const file_content ReadFile(const file_path& to_read)
  10.         {
  11.             std::ifstream file_stream(to_read);
  12.             std::string line_data;
  13.             std::vector<std::string> file_data;
  14.  
  15.             if (file_stream.is_open())
  16.             {
  17.                 while (std::getline(file_stream, line_data))
  18.                     file_data.push_back(line_data);
  19.  
  20.                 return file_data;
  21.             }
  22.  
  23.             return { "\0" };
  24.         }
  25.  
  26.  
  27.         // WriteFile
  28.         const bool WriteFile(const file_path& to_write, file_content& data)
  29.         {
  30.             std::ofstream file_stream(to_write);
  31.  
  32.             if (file_stream.is_open())
  33.             {
  34.                 for (auto& line_in : data)
  35.                     file_stream << line_in << "\n";
  36.  
  37.                 return true;
  38.             }
  39.  
  40.             return false;
  41.         }
  42.  
  43.  
  44.         // CreateFile
  45.         const bool CreateFile(const file_path& to_create)
  46.         {
  47.  
  48.             if (!CheckFile(to_create))
  49.                 outfile file_stream(to_create, std::ios::app);
  50.  
  51.             if (CheckFile(to_create))
  52.                 return true;
  53.  
  54.             return false;
  55.         }
  56.  
  57.  
  58.         // DeleteFile
  59.         const bool DeleteFile(const file_path& to_delete)
  60.         {
  61.             file file_stream(to_delete);
  62.  
  63.             if (file_stream.is_open())
  64.             {
  65.                 file_stream.close();
  66.                 std::remove(to_delete.c_str());
  67.                 return true;
  68.             }
  69.  
  70.             return false;
  71.         }
  72.  
  73.  
  74.         // CheckFile
  75.         const bool CheckFile(const file_path& to_check)
  76.         {
  77.             std::fstream file_stream(to_check);
  78.  
  79.             if (file_stream.is_open())
  80.                 return true;
  81.  
  82.             return false;
  83.         }
  84.     }
  85.  
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement