用c++编写一个简单的文件管理程序采用二级目录实现文件管理
时间: 2024-02-21 19:56:53 浏览: 120
C++语言实现一个多级文件目录管理系统,采用链表的数据结构。.zip
这里提供一个简单的二级目录文件管理程序的实现,包括添加文件、删除文件和查看文件列表等功能。
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class File {
public:
File(const std::string& name, int size, const std::string& content);
std::string getName() const;
int getSize() const;
std::string getContent() const;
private:
std::string name_;
int size_;
std::string content_;
};
class Directory {
public:
Directory(const std::string& name);
std::string getName() const;
void addFile(const File& file);
void removeFile(const std::string& name);
void printFiles() const;
private:
std::string name_;
std::vector<File> files_;
};
class FileSystem {
public:
FileSystem();
void addFile(const std::string& path, const File& file);
void removeFile(const std::string& path, const std::string& name);
void printFiles(const std::string& path) const;
private:
Directory root_;
Directory* findDirectory(const std::string& path) const;
File* findFile(const std::string& path, const std::string& name) const;
};
File::File(const std::string& name, int size, const std::string& content) :
name_(name), size_(size), content_(content) {}
std::string File::getName() const {
return name_;
}
int File::getSize() const {
return size_;
}
std::string File::getContent() const {
return content_;
}
Directory::Directory(const std::string& name) : name_(name) {}
std::string Directory::getName() const {
return name_;
}
void Directory::addFile(const File& file) {
files_.push_back(file);
}
void Directory::removeFile(const std::string& name) {
for (auto it = files_.begin(); it != files_.end(); ++it) {
if (it->getName() == name) {
files_.erase(it);
break;
}
}
}
void Directory::printFiles() const {
std::cout << "Files in directory " << name_ << ":\n";
for (const auto& file : files_) {
std::cout << "- " << file.getName() << " (" << file.getSize() << " bytes)\n";
}
}
FileSystem::FileSystem() : root_("root") {}
void FileSystem::addFile(const std::string& path, const File& file) {
int idx = path.find('/');
std::string dirName = path.substr(0, idx);
std::string fileName = path.substr(idx + 1);
Directory* dir = findDirectory(dirName);
if (!dir) {
std::cout << "Directory " << dirName << " not found.\n";
return;
}
dir->addFile(file);
}
void FileSystem::removeFile(const std::string& path, const std::string& name) {
int idx = path.find('/');
std::string dirName = path.substr(0, idx);
std::string fileName = path.substr(idx + 1);
Directory* dir = findDirectory(dirName);
if (!dir) {
std::cout << "Directory " << dirName << " not found.\n";
return;
}
dir->removeFile(name);
}
void FileSystem::printFiles(const std::string& path) const {
Directory* dir = findDirectory(path);
if (!dir) {
std::cout << "Directory " << path << " not found.\n";
return;
}
dir->printFiles();
}
Directory* FileSystem::findDirectory(const std::string& path) const {
if (path == "root") {
return const_cast<Directory*>(&root_);
}
int idx = path.find('/');
std::string dirName = path.substr(0, idx);
std::string subPath = path.substr(idx + 1);
for (auto& dir : root_.subDirectories_) {
if (dir.getName() == dirName) {
if (subPath.empty()) {
return const_cast<Directory*>(&dir);
} else {
return dir.findDirectory(subPath);
}
}
}
return nullptr;
}
File* FileSystem::findFile(const std::string& path, const std::string& name) const {
int idx = path.find('/');
std::string dirName = path.substr(0, idx);
std::string fileName = path.substr(idx + 1);
Directory* dir = findDirectory(dirName);
if (!dir) {
return nullptr;
}
for (auto& file : dir->files_) {
if (file.getName() == name) {
return const_cast<File*>(&file);
}
}
return nullptr;
}
int main() {
FileSystem fs;
fs.addFile("root/documents", File("report.docx", 1024, "This is a report."));
fs.addFile("root/pictures", File("photo.jpg", 2048, "This is a photo."));
fs.printFiles("root/documents");
fs.removeFile("root/documents", "report.docx");
fs.printFiles("root/documents");
return 0;
}
```
上面的代码实现了一个基本的二级目录文件管理系统,可以根据需要进行扩展。注意,这里只是提供了一种简单的实现方式,实际上实现一个完整的文件管理系统需要考虑更多的细节和功能。
阅读全文