用链表在多个函数中使用文件



我正在编写一个简单的"数据库"程序,只使用文本文件。我首先创建了createDB函数,以便用户可以命名他们想要创建的文件。为了确保这个名字是唯一的,并且没有一个已经有这个名字的文件,我把这个名字放在一个链表中。我在我的openDB函数中也使用链表来确保有一个该名称的文件要打开。我遇到的问题是每次程序启动时,链表都是空的,我想知道是否有办法使它保持其内容。这样我就可以创建一个文件,关闭我的程序,然后启动该程序并再次打开该文件,而无需创建新文件。如果我在文件夹里放了一个满是文本的文件,如果我创建了一个文件,并以同样的名字命名它就会用一个空白文件覆盖那个文件我不知道该怎么做。可能有比链表更好的方法来做所有这些,建议会很好。如有任何帮助,不胜感激。

我只是把我所有的代码在这里,仍然有很多空函数,但我只担心创建和打开到现在,谢谢你!

#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
class List{
private:
    struct dataB{ //node
        string name;
        int open; //1 if open 0 if closed
        dataB *next;
    };
    // initializing node variables to go through linked list and search
    dataB *head;
    dataB *curr;
    dataB *temp;
public:
    List();
    void insert(string name, int open);
    bool search(string fName);
    void createDB();
    void openDB();
    int menu();
}; //end class
List::List(){
    head = NULL;
    curr = NULL;
    temp = NULL;
}
void List::insert(string name, int open){
    dataB *n = new dataB;
    n->next = NULL;
    n->name = name;
    n->open = open;
    if(head != NULL){ // if already things in list put it last
        curr = head;
        while(curr->next != NULL){
            curr = curr->next;
        }
        curr->next = n; // always puts new node at the end
    }
    else{ // if no list, make new node the start of list
        head = n;
    }
}
bool List::search(string fName){ //return false if no match, true if there is
    curr = head; //start from beginning of list
    while(curr != NULL) {
        if (fName == curr->name){
            return true;
        }
    }
    return false;
}

void List::createDB() {
    ofstream db;
    string fileName;
    cout << "Enter the name of the database you want to create: n";
    getline (cin, fileName);
    if(this->search(fileName) == false){ // means new filename, create db
        db.open(fileName.c_str());
        cout << "nYour database " << fileName << " was created successfullyn";
        this->insert(fileName, 0);
    }
    else{ // checking if the filename is taken
        cout << "nCould not create database because database name " << fileName << " is already takenn";
    }
    db.close();
}
void List::openDB() {
    // need to add check to see if one is already open
    ofstream db;
    string fileName;
    cout << "Enter the name of the database you want to open: n";
    getline (cin, fileName);
    if(this->search(fileName) == false){ // means file not found
                cout << "nThere is no database named " << fileName << " to openn";
    }
    else{ // checking if there is a file of that name to open
        cout << "nThe database " << fileName << " has been opened successfullyn";
        db.open(fileName.c_str());
        this->insert(fileName, 1);
    }
}
void closeDB() {
    cout << "The database _______ has been closed successfully";
}
void display() {
    cout << "Enter the ID of the employee you want to display: n";
}
void update() {
}
void report() {
}
void add() {
}
void del() {
}
int List::menu() {
    cout << "Enter the number of the operation you wish to perform (1-9)n"
    << "1. Create new databasen"
    << "2. Open databasen"
    << "3. Close databasen"
    << "4. Display recordn"
    << "5. Update recordn"
    << "6. Create reportn"
    << "7. Add a recordn"
    << "8. Delete a recordn"
    << "9. Quitn";
    int sel = 0;
    (std::cin >> sel).ignore();
    switch (sel) {
        case 1: createDB();
            menu(); // after creating file go back to list of options
            break;
        case 2: openDB();
            menu();
            break;
        case 3: closeDB();
            menu();
            break;
        case 4: display();
            break;
        case 5: update();
            break;
        case 6: report();
            break;
        case 7: add();
            break;
        case 8: del();
            break;
        case 9: return 0;
            break;
        default: cout << "Please try again and enter a valid numbernn";
            menu();
            break;
    }
    return true; // to avoid error saying control may reach end of non-void function
}

int main() {
    List list;
    list.menu();
    return 0;
}

一开始就不需要链表。只需检查文件系统中是否存在,例如 <stat(2)。>

相关内容

  • 没有找到相关文章