如何允许用户为创建的文本文件命名

How to allow user to name text file that is created c++

本文关键字:文本 文件 创建 何允许 用户      更新时间:2023-10-16

我正在尝试创建一个用户输入名称的文本文件。它应该只是创建文本文件,然后关闭它,然后通过终端反馈是否成功创建。如果我硬编码文件名,我能够弄清楚如何创建文件,但现在我正在尝试使用用户输入,我无法获得要创建的文件。现在它甚至不让我输入任何东西,它只在我调用函数时结束。谢谢你的帮助。

编辑:我不确定是否因为我的getline调用只是得到一个空行,然后结束,或者为什么它不会让我cin任何东西。

EEDIT:这是我的完整代码,我想我认为不工作的部分是,所以希望有人能告诉我为什么我的代码只是结束后,我选择1为我的开关情况。显然我的代码不完整,但这是我所拥有的一切。

#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
void createDB() {
    ofstream db;
    string filename;
    cout << "Enter the name of the database you want to create: n";
    getline (cin, filename);
    db.open(filename.c_str());
    if(!db) { // checking if the file could be opened
        cout << "nCould not create databasen";
    }// add more checks to make sure file doesn't have same name
    else {
        cout << "nYour database " << filename << " was created successfullyn";
    }
    db.close();
}
void openDB() { // just opens hard coded file for now
    // need to add check to see if one is already open
    cout << "Enter the name of the database you want to open: ";
    string line;
    //ifstream file (
}
void closeDB() {
}
void display() {
}
void update() {
}
void create() {
}
void add() {
}
void del() {
}
int 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;
    cin >> sel;
    switch (sel) {
        case 1: createDB();
            //menu(); // after creating file go back to list of options
            break;
        case 2: openDB();
            break;
        case 3: closeDB();
            break;
        case 4: display();
            break;
        case 5: update();
            break;
        case 6: create();
            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() {
    menu();
    return 0;
}

您发布的代码片段实际上有效,但由于您之前的代码,似乎getline跳过了输入。所以如果你把剩下的代码贴出来会很有帮助的。

因此,因为您在getline之前使用cin,您应该在cin >> sel;之后添加std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');

所以你的菜单功能应该是

int 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;
cin >> sel;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
switch (sel) {
case 1: createDB();
    //menu(); // after creating file go back to list of options
    break;
case 2: openDB();
    break;
case 3: closeDB();
    break;
case 4: display();
    break;
case 5: update();
    break;
case 6: create();
    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
}