Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. // Code Written and maintained by Daniel Jajliardo @ TheTechSphere
  2. // Copywrite 2017 Daniel Jajliardo @ TheTechSphere
  3. // This is a small utiliy file for File Reading and writing
  4. // Version 1.0 Updated 01/20/17 Daniel Jajliardo
  5.  
  6. // Usage:
  7. // #include "fileManager.h"
  8. //
  9. // fmanager::FileManager fman;
  10. // string fileAsString = fman.readfile_s("file.txt");
  11. // vector<string> fileAsVector = fman.readfile_v("file.txt");
  12. //
  13. // fman.writeFile ("file.txt", "Overwrite file contents with this.");
  14. // fman.appendFile("file.txt", "Append this content to file.");
  15.  
  16. #include <iostream>
  17. #include <fstream>
  18. #include <string>
  19. #include <vector>
  20.  
  21. #pragma once
  22.  
  23. using namespace std;
  24. namespace fmanager {
  25.  
  26. class FileManager {
  27. private:
  28. fstream fs;
  29.  
  30. public:
  31. FileManager() {}
  32. ~FileManager() {}
  33.  
  34. string readFile_s(string FileName) {
  35. string result = "";
  36. fs.open(FileName, fstream::in);
  37. char c;
  38. while (fs.get(c))
  39. result += c;
  40.  
  41. fs.close();
  42. return result;
  43. }
  44.  
  45. vector<string> readFile_v(string FileName) {
  46. vector<string> result;
  47. fs.open(FileName, fstream::in);
  48. string out;
  49. while (getline(fs, out))
  50. result.push_back(out);
  51. fs.close();
  52. return result;
  53. }
  54.  
  55. void writeFile(string filename, string content) {
  56. ofstream fo;
  57. fo.open(filename, fstream::out);
  58. fo << content << "\n";
  59. fo.close();
  60. }
  61.  
  62. void appendFile(string filename, string content) {
  63. fs.open(filename, fstream::app);
  64. fs << content << "\n";
  65. fs.close();
  66. }
  67.  
  68. };
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement