Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Purpose: Create a program that allows combining all files in a directory into one singular file.
- How To Use: Place file within the SAME directory as all of your files. Also, this is for that single directory, NOT recursive.
- C++ Version Requirement: C++17 or higher.
- Algorithm Steps:
- 1. Set path all files are in.
- 2. Open one file at a time using directory_iterator.
- 3. For each file:
- a. Open file and make sure it is actually open. If not, send error msg and stop program.
- b. Call a function that gets data from the input file and stores it into a vector called fileLines.
- c. Call a function that stores the vector's data into an output file & closes it.
- */
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <vector>
- #include <filesystem>
- namespace fs = std::filesystem;
- // Function references
- void getInFileData(std::ifstream &inFile, std::vector<std::string> &v);
- void storeDataInNewFile(std::vector<std::string> &v);
- int main () {
- // 1. Set path all files are in.
- //fs::path p("C:\\Users\\Tom\\Desktop\\Anti-Shop-Exploits-v2\\1.15_Data\\recipes");
- // 2. Open one file at a time using directory_iterator.
- std::vector<std::string> fileLines;
- for (fs::directory_entry &elem : fs::directory_iterator()) {
- //for ( auto &elem : fs::directory_iterator(p) ) {
- // 3a. Open file and make sure it is actually open. If not, send error msg and stop program.
- std::ifstream inFile(elem);
- inFile.open(elem);
- if (!inFile.is_open()) {
- std::cout << "[Error] Could not open input file: " << elem << "\nStopping program...\n";
- inFile.close();
- exit(1);
- }
- // 3b. Call a function that gets data from the input file and stores it into a vector called fileLines.
- getInFileData(inFile, fileLines);
- // 3c. Call a function that stores the vector's data into an output file & closes it.
- storeDataInNewFile(fileLines);
- }
- return 0;
- }
- // 3b. Function Purpose: Get data from the input file and store it into referenced vector.
- void getInFileData(std::ifstream &inFile, std::vector<std::string> &v) {
- v.clear(); // Empty vector.
- // For each line in CURRENT file, store data into vector.
- for (int i = 0; std::getline(inFile, v[i]); ++i);
- // 2d. Once program has stored all of the file's lines into the vector, close inFile.
- inFile.close();
- }
- // 3c. Function Purpose: Store the vector's data into an output file & close it.
- // - Declare path.
- // - Open path / output file.
- // - Make sure it is open.
- // - Store all data from vector into output file using a loop.
- // - Create a new line to indicate the end of a file and the beginning of a new file (within the output file).
- // - Close File.
- void storeDataInNewFile(std::vector<std::string> &v) {
- // Declare path.
- fs::path filePath("C:\\Users\\Tom\\Desktop\\Anti-Shop-Exploits-v2\\1.15_Data\\recipes\\allData.json"); // Absolute (Full) Path
- // Open path / output file.
- std::ofstream outFile(fs::absolute(filePath));
- // Make sure it is open.
- if (!outFile.is_open()) {
- std::cout << "[Error] Could not open output file: " << filePath << "\nStopping program...\n";
- exit(1);
- }
- // Store all data from vector into output file using a loop.
- const int vSize = v.size();
- for (int i = 0; i < vSize; ++i) {
- outFile << v[i];
- }
- // Create a new line to indicate the end of a file and the beginning of a new file (within the output file).
- outFile << "\n";
- outFile.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment