[C++/Repactoring] How can I use of stream as a class member?

Asked 2 years ago, Updated 2 years ago, 23 views

The code below is a code that works, but it's too legible. It seems that the file stream part is divided and it is also due to overlapping parts.

My idea is to use the part of the file stream as a class member variable? (function?) and make the part that checks if the file can be opened as a function, but I'm a beginner, so I don't know how to do it.

void RegisterAccount::createAccount() {
    ofstream accountFile("pass.dat", ios::out /*| ios::binary */ | ios::app); // File to which ID and encrypted password will be logged
    if (!accountFile) {
        cerr << "File could not be opened" << endl;
        exit(EXIT_FAILURE);
    } // How can I clean up the code in the file stream?

    If (inputID() == true) { // when the userID enters getID()

        string fileNameToUserName = (getID() + ".dat"); 
        offstream userInfoFile(fileNameToUserName.c_str(), ios::out /*| ios:binary*/ | ios::app); // ID, email, account permissions, file to which reservation history will be logged
        if (!userInfoFile) {
            cerr << "File could not be opened" << endl;
            exit(EXIT_FAILURE);
        }   // How can I clean up the code in the file stream?

        if (inputPW() == true) {
            if (inputEmail() == true) {
                accountFile << getID() << endl;
                accountFile <<cipher(getPW()) <<endl; // Encrypt and enter into the file

                userInfoFile << getID() << endl;
                userInfoFile << getEmail() << endl;
                userInfoFile << getAuthInfo() << endl;
            }
        }
    }
    accountFile.close();
}

c++

2022-09-22 20:22

1 Answers

Try ~ catch ~ finally try to organize it.

Create a function by opening the
// file and throwing an exception if it fails. (It would be roughly as follows.))
static void openFile(ofstream& os, char const * const filename) {
   os.open(filename,ios::out|ios::app);
    if (!os.is_open()) {
        throw string("File could not be opened");
    } 
}

void RegisterAccount::createAccount() {
  ofstream accountFile, userInfoFile;
  try {
    openFile(accountFile, "pass.dat")

    If (inputID() == true) { // when the userID enters getID()

        string fileNameToUserName = (getID() + ".dat"); 
        openFile(userInfoFile,fileNameToUserName.c_str());
        if (inputPW() == true) {
            if (inputEmail() == true) {
                accountFile << getID() << endl;
                accountFile <<cipher(getPW()) <<endl; // Encrypt and enter into the file

                userInfoFile << getID() << endl;
                userInfoFile << getEmail() << endl;
                userInfoFile << getAuthInfo() << endl;
            }
        }
    }
  } } catch(string err) {
    // Exception handling when file is not open.
    // If necessary, you can create an exception type that stores exception information.
    cerr << err << endl;
    exit(EXIT_FAILURE);
  } } finally {
    if(accountFile.is_open()) accountFile.close();
    if(userInfoFile.is_open()) userInfoFile.close();
  }
}


2022-09-22 20:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.