gpt-4-0314· OpenAI WildChat-4.8M (AI2)
Улучши код и функции. алгоритм сканирования должны быть максимально эффективными и…
- User
Улучши код и функции. алгоритм сканирования должны быть максимально эффективными и актуальными
#include #include #include <dirent.h> #include #include #include
using namespace std;
bool isVirus(string fileName, string databaseFilePath) { // Открываем файл и считываем его содержимое в буфер ifstream file(fileName); string buffer((istreambuf_iterator(file)), istreambuf_iterator());
// Получаем список вирусов из базы данных ifstream databaseFile(databaseFilePath); vector<string> virusList; string line; while (getline(databaseFile, line)) { virusList.push_back(line); } // Сравниваем содержимое файла с базой данных вирусов for (string virus : virusList) { if (buffer == virus) { return true; } } return false;}
bool deleteFile(string fileName) { if (remove(fileName.c_str()) != 0) { return false; }
return true;}
vector getFilesInDirectory(string path) { vector files;
DIR* dir; struct dirent* ent; if ((dir = opendir(path.c_str())) != NULL) { while ((ent = readdir(dir)) != NULL) { if (ent->d_type == DT_REG) { // Только обычные файлы files.push_back(ent->d_name); } } closedir(dir); } return files;}
void scanDirectory(string path, string databaseFilePath) { vector files = getFilesInDirectory(path);
for (string fileName : files) { string fullFilePath = path + " / " + fileName; if (isVirus(fullFilePath, databaseFilePath)) { cout << "Файл " << fullFilePath << " является вирусом!" << endl; // Предлагаем удалить файл deleteFile(fullFilePath); } } // Рекурсивный вызов для всех поддиректорий DIR* dir; struct dirent* ent; if ((dir = opendir(path.c_str())) != NULL) { while ((ent = readdir(dir)) != NULL) { if (ent->d_type == DT_DIR && strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "…") != 0) { // Только директории, кроме . и … string subDirPath = path + " / " + ent->d_name; scanDirectory(subDirPath, databaseFilePath); } } closedir(dir); }}
int main() { // Путь к директории, которую нужно просканировать string path = " / path / to / my / directory";
// Путь к файлу базы данных вирусов string databaseFilePath = " / path / to / my / database.txt"; // Сканируем директорию на наличие вирусов scanDirectory(path, databaseFilePath); return 0;}
- Assistant
#include <iostream> #include <fstream> #include <dirent.h> #include <string> #include <vector> #include <cstdio> #include <cstring> using namespace std; // Читает файл-БД и помещает содержимое в вектор vector<string> readDatabaseFile(const string& databaseFilePath) { vector<string> virusList; ifstream databaseFile(databaseFilePath); string line; while (getline(databaseFile, line)) { virusList.push_back(line); } return virusList; } bool isVirus(const string& buffer, const vector<string>& virusList) { for (const string& virus : virusList) { if (buffer == virus) { return true; } } return false; } string readBufferFromFile(const string& fileName) { ifstream file(fileName); return string((istreambuf_iterator<char>(file)), istreambuf_iterator<char>()); } bool deleteFile(const string& fileName) { return remove(fileName.c_str()) == 0; } vector<string> getFilesInDirectory(const string& path) { vector<string> files; DIR* dir; struct dirent* ent; if ((dir = opendir(path.c_str())) != NULL) { while ((ent = readdir(dir)) != NULL) { if (ent->d_type == DT_REG) { // Только обычные файлы files.push_back(ent->d_name); } } closedir(dir); } return files; } void scanDirectory(const string& path, const vector<string>& virusList) { vector<string> files = getFilesInDirectory(path); for (const string& fileName : files) { string fullFilePath = path + "/" + fileName; string buffer = readBufferFromFile(fullFilePath); if (isVirus(buffer, virusList)) { cout << "Файл " << fullFilePath << " является вирусом!" << endl; // Предлагаем удалить файл if(deleteFile(fullFilePath)) { cout << "Файл " << fullFilePath << " удален!" << endl; } else { cout << "Ошибка при удалении файла " << fullFilePath << "!" << endl; } } } // Рекурсивный вызов для всех поддиректорий DIR* dir; struct dirent* ent; if ((dir = opendir(path.c_str())) != NULL) { while ((ent = readdir(dir)) != NULL) { if (ent->d_type == DT_DIR && strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0) { // Только директории, кроме . и .. string subDirPath = path + "/" + ent->d_name; scanDirectory(subDirPath, virusList); } } closedir(dir); } } int main() { // Путь к директории, которую нужно просканировать string path = "/path/to/my/directory"; // Путь к файлу базы данных вирусов string databaseFilePath = "/path/to/my/database.txt"; // Читаем файл-БД и сразу помещаем данные в вектор vector<string> virusList = readDatabaseFile(databaseFilePath); // Сканируем директорию на наличие вирусов scanDirectory(path, virusList); return 0; }