使用链接列表

时间:2016-09-12 01:42:07

标签: c++ file-io linked-list

我正在编写一个只使用文本文件的简单“数据库”程序。我首先创建了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 successfully\n";
        this->insert(fileName, 0);
    }
    else{ // checking if the filename is taken
        cout << "\nCould not create database because database name " << fileName << " is already taken\n";
    }

    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 open\n";
    }

    else{ // checking if there is a file of that name to open
        cout << "\nThe database " << fileName << " has been opened successfully\n";
        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 database\n"
    << "2. Open database\n"
    << "3. Close database\n"
    << "4. Display record\n"
    << "5. Update record\n"
    << "6. Create report\n"
    << "7. Add a record\n"
    << "8. Delete a record\n"
    << "9. Quit\n";

    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 number\n\n";
            menu();
            break;
    }
    return true; // to avoid error saying control may reach end of non-void function
}


int main() {
    List list;
    list.menu();

    return 0;
}

1 个答案:

答案 0 :(得分:-1)

您首先不需要链接列表。只需检查文件系统中是否存在,例如stat(2)