Advertisement
Guest User

Files.cpp

a guest
Jan 17th, 2019
74
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 files {
  5.  
  6.  
  7.     // ReadFile : (types::file_path) : types::file_content
  8.     types::file_content ReadFile(types::file_path to_read) {
  9.         types::infile file_stream(to_read);
  10.         types::file_line ldata;
  11.         types::file_content fdata;
  12.  
  13.         if (file_stream.is_open())
  14.             while (std::getline(file_stream, ldata))
  15.                 fdata.push_back(ldata);
  16.  
  17.         return fdata;
  18.     }
  19.  
  20.     // WriteFile : (types::file_path, types::file_content) : bool
  21.     bool WriteFile(types::file_path to_write, types::file_content fdata) {
  22.         types::outfile file_stream(to_write);
  23.  
  24.         if (file_stream.is_open())
  25.             for (auto& data : fdata)
  26.                 file_stream << data << "\n";
  27.  
  28.         else return false;
  29.         return true;
  30.     }
  31.  
  32.     // CreateFile : (types::file_path) : bool
  33.     bool CreateFile(types::file_path to_create) {
  34.  
  35.         if (!CheckFile(to_create))
  36.             types::outfile file_stream(to_create, std::ios::app);
  37.  
  38.         if (!CheckFile(to_create)) return false;
  39.         return true;
  40.     }
  41.  
  42.     // DeleteFile : (types::file_path) : bool
  43.     bool DeleteFile(types::file_path to_delete) {
  44.         types::file file_stream(to_delete);
  45.  
  46.         if (file_stream.is_open()) {
  47.             file_stream.close();
  48.             std::remove(to_delete.c_str());
  49.             return true;
  50.         }
  51.  
  52.         return false;
  53.     }
  54.  
  55.     // CheckFile : (types::file_path) : bool
  56.     bool CheckFile(types::file_path to_check) {
  57.         types::file file_stream(to_check);
  58.  
  59.         if (!file_stream.is_open()) return false;
  60.         return true;
  61.     }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement