简单模拟文件系统的目录组织,多节点树的应用,为求简单,没有进行错误处理。代码如下:
#ifndef __CFILEDIRECTORY__H
#define __CFILEDIRECTORY__H
#include <iostream>
#include <queue>
// 文件系统目录结构
const int MAX_LEN = 128;
struct TreeNode {
char szName[MAX_LEN]; // 文件或者目录的名字
TreeNode* pFirstChild; // 第一个儿子节点
TreeNode* pNextSibling; // 第一个儿子节点的兄弟节点
TreeNode()
{
memset(szName, 0, sizeof(szName));
pFirstChild = NULL;
pNextSibling = NULL;
}
};
class CFileDirectory
{
public:
CFileDirectory(char* strName); // 根目录
~CFileDirectory();
public:
bool MakeDirectory(char* strParentDir, char* strDirName); // 创建目录
bool MakeFile(char* strPath, char* strFileName); // 在路径下创建文件
void PrintAllFile(); // 打印所有文件
private:
TreeNode* FindNode(TreeNode* pNode, char* strName); //寻找节点
void PrintAllFile(TreeNode* pNode, std::queue<std::string>& queueNodeName); // 打印所有文件
void DeleteNode(TreeNode*& pNode);
private:
struct TreeNode* m_pRoot; // 文件路径根目录
};
#endif
#define _CRT_SECURE_NO_WARNINGS #include "FileDirectory.h" #include <string.h> CFileDirectory::CFileDirectory(char* strName) // 根目录 { m_pRoot = new struct TreeNode; strcpy(m_pRoot->szName, strName); } CFileDirectory::~CFileDirectory() { DeleteNode(m_pRoot); } bool CFileDirectory::MakeDirectory(char* strParentDir, char* strDirName) { if (strParentDir[0] != '/') // 第一个必须是 /是根目录 return false; strParentDir++; // 跳过根目录 char* pTemp = strParentDir; TreeNode* pFind = m_pRoot; int iFindeIndex = 0; for (int i = 0; i < strlen(pTemp); i++) // 从前往后找 { if (*(pTemp + i) == '/') { char pDirName[128] = { 0x00 }; memcpy(pDirName, pTemp + iFindeIndex, i - iFindeIndex); iFindeIndex = i; pFind = FindNode(pFind, pDirName); if (!pFind) return false; } } // 创建目录 if (!pFind->pFirstChild) // 没有第一个孩子节点 创建 { TreeNode* pNode = new TreeNode; strcpy(pNode->szName, strDirName); pFind->pFirstChild = pNode; return true; } TreeNode* pNodeSlib = pFind->pFirstChild->pNextSibling; if (!pNodeSlib) { TreeNode* pNode = new TreeNode; strcpy(pNode->szName, strDirName); pFind->pFirstChild->pNextSibling = pNode; return true; } else { while (pNodeSlib->pNextSibling) pNodeSlib = pNodeSlib->pNextSibling; TreeNode* pNode = new TreeNode; strcpy(pNode->szName, strDirName); pNodeSlib->pNextSibling = pNode; return true; } } bool CFileDirectory::MakeFile(char* strPath, char* strFileName) { return MakeDirectory(strPath, strFileName); } TreeNode* CFileDirectory::FindNode(TreeNode* pNode, char* strName) { if (!pNode) return NULL; if (strcmp(pNode->szName, strName) == 0) return pNode; TreeNode* pTemp; pTemp = FindNode(pNode->pFirstChild, strName); if (!pTemp) pTemp = FindNode(pNode->pNextSibling, strName); return pTemp; } void CFileDirectory::PrintAllFile() // 打印所有文件 { std::queue<std::string> queueNode; PrintAllFile(m_pRoot, queueNode); } void CFileDirectory::PrintAllFile(TreeNode* pNode, std::queue<std::string>& queueNodeName) { if (!pNode->pFirstChild) // 没有节点了 是文件 打印 { std::queue<std::string> queueAfter; queueAfter = queueNodeName; // 保存父目录 queueNodeName.push(pNode->szName); while (!queueNodeName.empty()) { std::string str = queueNodeName.front(); std::cout << str.c_str() << "/"; queueNodeName.pop(); } queueNodeName.swap(queueAfter); // 交换 std::cout << std::endl; } if (pNode->pFirstChild) { queueNodeName.push(pNode->szName); PrintAllFile(pNode->pFirstChild, queueNodeName); } if (pNode->pNextSibling) PrintAllFile(pNode->pNextSibling, queueNodeName); } void CFileDirectory::DeleteNode(TreeNode*& pNode) { if (!pNode) return; DeleteNode(pNode->pFirstChild); DeleteNode(pNode->pNextSibling); delete pNode; pNode = NULL; return; }
#include <iostream> #include "FileDirectory.h" int main() { CFileDirectory *fileSystem = new CFileDirectory("/"); fileSystem->MakeDirectory("/", "user"); fileSystem->MakeFile("/user/", "password.txt"); fileSystem->MakeFile("/user/", "hello.txt"); fileSystem->MakeFile("/user/", "world.txt"); fileSystem->MakeDirectory("/", "log"); fileSystem->PrintAllFile(); delete fileSystem; std::cin.get(); }