用链表在多个函数中使用文件
using files in multiple functions with 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 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)
。>
相关文章:
- C++ gmock - 我们如何在单元测试 cpp 文件中读取/获取 cpp 文件函数的参数值
- 使用其他头文件函数的头文件
- 创建文件函数是否可以打开仅在WinObj实用程序中的全局目录下列出的设备的句柄?
- 如何指定创建文件函数获取未缓存的结果?
- 标准库头文件函数原型的实现是如何用 c++ 编写的?
- 如何将类对象作为对另一个头文件函数的引用传递
- 读取文件函数lpbuffer和字节以读取澄清
- C++ - 未定义的对多个文件函数的引用
- 使用C++的可视 Unicode 文件函数
- C 创建文件函数错误
- WinAPI 创建文件函数 2.
- 具有CREATE_ALWAYS处置的创建文件函数
- 重写c库文件函数
- 如何使用客户端调用堆栈中的地址从 PDB 检索文件/函数/行号?
- 通过头文件函数将遍历的数据从二进制搜索树加载到Vector中
- 需要帮助无法弄清楚为什么我的头文件函数声明不起作用
- 是否可以在不使用文件函数的情况下使用代码块从文件运行某些用例测试?
- 如何用c++文件函数读写8位整数单位形式的数据
- 在c++中读文件函数如何识别文本文件的结束
- 使用Windows API的c++文件函数