Advertisement
Tark_Wight

cppFileCleaner

Aug 11th, 2023 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | Software | 0 0
  1. /*
  2. Писал приложение под себя. Нужно было очистить файлы с заданиями для учеников от рабочего кода,
  3. оставляя только условие задач.
  4. Требует С++15
  5. */
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. #include <filesystem>
  10.  
  11. int main() {
  12.     std::string delimiter = " //  //  //  //  //  //  //  Solution  //  //  //  //  //  //  //";
  13.    
  14.     // Получаем текущую директорию
  15.     std::filesystem::path currentDir = std::filesystem::current_path();
  16.    
  17.     // Создаем папку "clean solution" в текущей директории, если она не существует
  18.     std::filesystem::path cleanDir = currentDir / "clean solution";
  19.     if (!std::filesystem::exists(cleanDir)) {
  20.         std::filesystem::create_directory(cleanDir);
  21.     }
  22.    
  23.     // Проходим по всем файлам с расширением ".cpp" в текущей директории
  24.     for (const auto& entry : std::filesystem::directory_iterator(currentDir)) {
  25.         if (entry.path().extension() == ".cpp") {
  26.             std::ifstream inputFile(entry.path());
  27.             std::ofstream outputFile(cleanDir / entry.path().filename());
  28.  
  29.             bool write = true;
  30.             std::string line;
  31.             while (std::getline(inputFile, line)) {
  32.                 if (line.find(delimiter) != std::string::npos) {
  33.                     write = false;
  34.                 }
  35.  
  36.                 if (write) {
  37.                     outputFile << line << '\n';
  38.                 }
  39.             }
  40.         }
  41.     }
  42.    
  43.     std::cout << "Cleaning and saving completed." << std::endl;
  44.    
  45.     return 0;
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement