简体   繁体   中英

file i/o between multiple functions c++

I am having trouble figuring out how to close a file using a close function for a file that I have opened in a separate function. Right now my program consists mainly of 3 functions, create, open, and close. Create and open are working fine, but after I open the file and go back to my menu of options I am trying to be able to close the file without having any user input. I want the close function to detect which file is open, and close it. There should only be one text file allowed to be open at a time in my open (which I havent been able to code but I assume works the same was as the close function). Here is all my code below, there are other functions I still need to implement but I am only worried about the first three right now. Thanks for any help!

#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);

    string fullFile = fileName + ".txt";

    std::ifstream fin(fullFile);

    if(fin.good()){ // means filename already exists
       cout << "\nCould not create database because database name " << fullFile << " is already taken\n";
    }
    else{ // creates file
        cout << "\nYour database " << fullFile << " was created successfully\n";
        db.open(fullFile);
    }

    db.close();
}

void openDB() {
    // need to add check to see if one is already open
    string fileName;

    cout << "Enter the name of the database you want to open: \n";
    getline (cin, fileName);

    string fullFile = fileName + ".txt";

    std::ifstream db(fullFile);

    if(db.good()){ // means file exists
        cout << "\nThe database " << fullFile << " has been opened successfully\n";
        db.open(fullFile);
    }

    else{ // there is no file named that to open
      cout << "\nThere is no database named " << fullFile << " to open\n";
    }
}


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 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() {

    menu();

return 0;
}

You must receive the file descriptor from the open file. c stile: FILE *myfile; myfile = fopen (myfile , "r"); you can keep reading in: http://www.cprogramming.com/tutorial/cfileio.html (I think that in c it is easier to understand and it is also works in c++). c++ stile http://www.cprogramming.com/tutorial/lesson10.html He explain it very good.

But you should remember: 1) you must create a pointer (this his the only way the data wont be lost). 2) you must send/receive the descriptor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM