递归保存/加载函数c ++

时间:2015-10-24 17:03:32

标签: c++ recursion filesystems

我用c ++创建了一个用来模拟硬盘的虚拟文件系统。我现在想弄清楚的是我如何“保存”这个硬盘驱动器,例如.txt然后再将整个东西导入程序。考虑到你可以在彼此内部创建无限量的文件夹和文件,这变得很复杂:/我现在拥有的递归函数不能以一种可行的方式将其加载回来。我会很高兴的有人看了看!

非常感谢!

这是我文件系统的.h文件。

#ifndef FOLDER_H
#define FOLDER_H
#include <iostream>
#include <string>
#include "file.h"
using namespace std;
class folder
{
private:
    int CAP1 = 10;
    int CAP2 = 10;

    int nrOfChildFolders;
    int nrOfFiles;
    string folderName;
    folder * parentFolder;
    folder ** childFolder = new folder *[CAP1];
    file ** fileArray = new file * [CAP2];



public:
    folder();
    folder(string folderName, folder * parent);
    ~folder();
    void addFolder(string folderName);
    void addFile(string fileName, int nr);
    string getFolderAndFiles()const;
    string getFolderName()const;
    int getFileNr(string fileName)const;
    void reset();
    folder * getFolder(string name)const; 
    folder * getParentFolder()const;
    void deleteFile(string name, int nr);
    folder * getChildFolder(int pos);
    int getNrOfChildFolders()const;
    int getNrOfFiles()const;
    file * getFile(int pos);
    folder& operator=(const folder& other);
    /* data */
};
#endif

这是我的递归函数:

void FileSystem::recursiveSave(folder * newCurrentFolder, string &saveFile)
{
    ofstream file;
    file.open(saveFile, fstream::app);
    folder * child;
    for (int i = 0; i < newCurrentFolder->getNrOfChildFolders(); i++)
    {
        file << newCurrentFolder->getChildFolder(i)->getFolderName() << "\n";
        nrOfLines++;
        child = newCurrentFolder->getChildFolder(i);
        recursiveSave(child, saveFile); 
    }
    file << newCurrentFolder->getNrOfChildFolders() << "\n"; 
    nrOfLines++; 
    for (int i = 0; i < newCurrentFolder->getNrOfFiles(); i++) 
    {
        file << newCurrentFolder->getFile(i)->returnValue() << "\n";
        nrOfLines++;
        file << newCurrentFolder->getFile(i)->returnName() << "\n"; nrOfLines++; 
    }
    file << newCurrentFolder->getNrOfFiles() << "\n"; nrOfLines++; 
}

0 个答案:

没有答案