Guest User

Untitled

a guest
May 2nd, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. /*
  2. Purpose: Create a program that allows combining all files in a directory into one singular file.
  3. How To Use: Place file within the SAME directory as all of your files. Also, this is for that single directory, NOT recursive.
  4. C++ Version Requirement: C++17 or higher.
  5.  
  6. Algorithm Steps:
  7.     1. Set path all files are in.
  8.     2. Open one file at a time using directory_iterator.
  9.     3. For each file:
  10.         a. Open file and make sure it is actually open. If not, send error msg and stop program.
  11.         b. Call a function that gets data from the input file and stores it into a vector called fileLines.
  12.         c. Call a function that stores the vector's data into an output file & closes it.
  13.  
  14. */
  15. #include <iostream>
  16. #include <string>
  17. #include <fstream>
  18. #include <vector>
  19. #include <filesystem>
  20. namespace fs = std::filesystem;
  21.  
  22. // Function references
  23. void getInFileData(std::ifstream &inFile, std::vector<std::string> &v);
  24. void storeDataInNewFile(std::vector<std::string> &v);
  25.  
  26. int main () {
  27.     // 1. Set path all files are in.
  28.     //fs::path p("C:\\Users\\Tom\\Desktop\\Anti-Shop-Exploits-v2\\1.15_Data\\recipes");
  29.    
  30.     // 2. Open one file at a time using directory_iterator.
  31.     std::vector<std::string> fileLines;
  32.     for (fs::directory_entry &elem : fs::directory_iterator()) {
  33.     //for ( auto &elem : fs::directory_iterator(p) ) {
  34.         // 3a. Open file and make sure it is actually open. If not, send error msg and stop program.
  35.         std::ifstream inFile(elem);
  36.         inFile.open(elem);
  37.  
  38.         if (!inFile.is_open()) {
  39.             std::cout << "[Error] Could not open input file: " << elem << "\nStopping program...\n";
  40.             inFile.close();
  41.             exit(1);
  42.         }
  43.  
  44.         // 3b. Call a function that gets data from the input file and stores it into a vector called fileLines.
  45.         getInFileData(inFile, fileLines);
  46.  
  47.         // 3c. Call a function that stores the vector's data into an output file & closes it.
  48.         storeDataInNewFile(fileLines);
  49.     }
  50.  
  51.  
  52.     return 0;
  53. }
  54.  
  55.  
  56. // 3b. Function Purpose: Get data from the input file and store it into referenced vector.
  57. void getInFileData(std::ifstream &inFile, std::vector<std::string> &v) {
  58.     v.clear(); // Empty vector.
  59.  
  60.     // For each line in CURRENT file, store data into vector.
  61.     for (int i = 0; std::getline(inFile, v[i]); ++i);
  62.  
  63.     // 2d. Once program has stored all of the file's lines into the vector, close inFile.
  64.     inFile.close();
  65. }
  66.  
  67.  
  68. // 3c. Function Purpose: Store the vector's data into an output file & close it.
  69. //      - Declare path.
  70. //      - Open path / output file.
  71. //      - Make sure it is open.
  72. //      - Store all data from vector into output file using a loop.
  73. //      - Create a new line to indicate the end of a file and the beginning of a new file (within the output file).
  74. //      - Close File.
  75. void storeDataInNewFile(std::vector<std::string> &v) {
  76.     // Declare path.
  77.     fs::path filePath("C:\\Users\\Tom\\Desktop\\Anti-Shop-Exploits-v2\\1.15_Data\\recipes\\allData.json"); // Absolute (Full) Path
  78.  
  79.     // Open path / output file.
  80.     std::ofstream outFile(fs::absolute(filePath));
  81.  
  82.     // Make sure it is open.
  83.     if (!outFile.is_open()) {
  84.         std::cout << "[Error] Could not open output file: " << filePath << "\nStopping program...\n";
  85.         exit(1);
  86.     }
  87.  
  88.     // Store all data from vector into output file using a loop.
  89.     const int vSize = v.size();
  90.     for (int i = 0; i < vSize; ++i) {
  91.         outFile << v[i];
  92.     }
  93.  
  94.     // Create a new line to indicate the end of a file and the beginning of a new file (within the output file).
  95.     outFile << "\n";
  96.  
  97.     outFile.close();
  98. }
Advertisement
Add Comment
Please, Sign In to add comment