2016-09-12 7 views
-2

テキストファイルを使用する単純な "データベース"プログラムを作成しています。最初に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; 
} 
+0

実際にリンクリストが必要だった場合、なぜあなたは自分自身を実装しようと時間を無駄にしないように 'std :: list'や' std :: forward_list'を使いませんでしたか? – PaulMcKenzie

答えて

-1

最初にリンクリストは必要ありません。ファイルがのファイルシステムに存在するかどうかを確認するには、stat(2)などがあります。

+0

@downvoterああやってよ。 – EJP

関連する問題