gpt4 book ai didi

c++ - 链表和文件 I/O C++

转载 作者:行者123 更新时间:2023-11-28 05:31:26 25 4
gpt4 key购买 nike

我正在尝试制作一个允许用户命名文件然后创建该文件的程序。当我需要错误检查以确保不存在具有该名称的文件时,我的问题就来了。我决定将文件名放入一个链接列表中,然后每次创建一个文件后,它都会遍历该列表以确保新文件名与以前的文件名不匹配。我的代码中还有其他功能,但我只担心让 createDB 功能正常工作,现在它总是说名称已经被使用,并且从进行一些调试来看,这似乎是因为我的 curr 变量始终为 NULL。任何帮助将不胜感激。

#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
bool match = true;
while(curr != NULL) {
if (fName != curr->name){
curr = curr->next;
match = false;
}
}
return match;
}


void List::createDB() {
ofstream db;
string fileName;
List list;
cout << "Enter the name of the database you want to create: \n";
getline (cin, fileName);

if(list.search(fileName) == false){ // means new filename, create db
db.open(fileName.c_str());
cout << "\nYour database " << fileName << " was created successfully\n";
list.insert(fileName, 0);
}
else if(list.search(fileName) == true) { // checking if the filename is taken
cout << "\nCould not create database because database name is already taken\n";
}
else {
cout << "There was a problem creating the database";
}

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: ";
getline (cin, filename);

db.open(filename.c_str());

}

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;
}

最佳答案

我看到的问题:

问题一

List::search() 的实现不正确。你有:

bool List::search(string fName){
curr = head;

// The initial value of match is true.
// When the list is empty, the function will return true.
// Not good.
bool match = true;
while(curr != NULL) {
if (fName != curr->name){
curr = curr->next;

// It is set to false when there is no match
// But it is not reset to true when there is a match.
match = false;
}
}
return match;
}

实现可以简单得多。

bool List::search(string fName){
curr = head;
while(curr != NULL) {
if (fName == curr->name){

// If a match is found, return true
// There is no need to continue any more checks.
return true;
}
}

// If we come here, there is no match.
// Return false.
return false;
}

问题2

每次调用 createDB() 时,您都在新的 List 中搜索文件。

你有:

void List::createDB() {
ofstream db;
string fileName;
List list;
...
}

线

   List list

每次调用 List::createDB() 时都会创建一个新的 List。因此,对该函数中的 list 对象的调用始终是函数本地对象,而不是调用 createDB() 的对象。

您需要在该函数中使用 this 而不是 list

void List::createDB() {
ofstream db;
string fileName;

// Don't need this.
// List list;
cout << "Enter the name of the database you want to create: \n";
getline (cin, fileName);

// Use this instead of list
// if(list.search(fileName) == false){ // means new filename, create db
if(this->search(fileName) == false){ // means new filename, create db
db.open(fileName.c_str());
cout << "\nYour database " << fileName << " was created successfully\n";

// Use this instead of list
// list.insert(fileName, 0);
this->insert(fileName, 0);
}

// You don't really need this->search() again. That function
// has already returned false.
// else if(list.search(fileName) == true) { // checking if the filename is taken
else{
cout << "\nCould not create database because database name is already taken\n";
}

// This block is not needed any more.
// else {
// cout << "There was a problem creating the database";
// }

db.close();
}

关于c++ - 链表和文件 I/O C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39432620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com