0xC0000005 Exception thrown when reading binary file (C++)

-1

I'm trying to have user login and registration system using binary files to save the data, and check if username and password are equal to those saved. It seems to work fine when Create_Login() and Write() are called and even when Login() is called after. The problem occurs when Login() is called by itself. The Exception gets thrown when it try's to read username and password. I've been trying to fix this for a while now and just cant see it. Any help would be appreciated

Here is my code

Main.cpp

int input;
cin >> input;

    switch (input) {
    case 1:
        file.Login("");
        break;

    case 2:
        file.Create_Login();
        break;
}

.h file

struct Files {
    char username[50];
    char password[50];


    string CheckPassword;
    string CheckUsername;

public:
    void Write(string name);
    void Login(string name);
    void Create_Login();

    Files();
    ~Files();

public:

    ifstream ReadFile; // Read
    ofstream WriteFile; // Write
};

.cpp file

Files::Files() {}

void Files::Login(string name) {
    cout << "\nEnter Username: " << flush; cin >> CheckUsername;
    cout << "Enter Password: " << flush; cin >> CheckPassword;

    name = CheckUsername;
    ReadFile.open(name + ".bin", ios::out | ios::binary);

   cout << "Your Username is: " << CheckUsername << ", " << "Your Password is: " << CheckPassword << endl; // debug line

    if (ReadFile.is_open()) {
        cout << "Opened file " + name + ".bin" << endl;

        ReadFile.read(reinterpret_cast<char*>(&username), sizeof(Files)); //Execption Thrown Here!!!!!
        ReadFile.read(reinterpret_cast<char*>(&password), sizeof(Files));
    
        if (CheckUsername == username && CheckPassword == password) {
            cout << "Access Granted" << endl;
        }
        else if (CheckUsername != username && CheckPassword != password) {
            cout << "Access Denined" << endl;
        }
        else {
            cout << "Error!!!" << endl;
        }
    }
    else {
        cout << "Could not open file: " + name << endl;
        cout << "File does not exist" << endl;
    } 
}

void Files::Create_Login() {
    cout << "Create UserName >> " << flush; cin >> username;
    cout << "\nCreate Password >> " << flush; cin >> password;

    Write(username);
}

void Files::Write(string name) {
    WriteFile.open(name + ".bin", ios::binary);

    if (WriteFile.is_open()) {
        WriteFile.write(reinterpret_cast<char*>(&username), sizeof(Files));
        WriteFile.write(reinterpret_cast<char*>(&password), sizeof(Files));
        WriteFile.close();
    }
    else {
        cout << "could not create file: " << name + ".bin" << endl;
    }
    Login(username);
}

Files::~Files() {
    cout << "Destructor Called" << endl;
    ReadFile.close();
}
c++
windows
visual-studio
binaryfiles
asked on Stack Overflow Apr 16, 2021 by Gamingninja5115

1 Answer

0

Exception resolved. by changing

ReadFile.read(reinterpret_cast<char*>(&username), sizeof(Files)); 
ReadFile.read(reinterpret_cast<char*>(&password), sizeof(Files));
 

to

 ReadFile.read(reinterpret_cast<char*>(&username), sizeof(username)); 
 ReadFile.read(reinterpret_cast<char*>(&password), sizeof(password));

thanks for the info

answered on Stack Overflow Apr 16, 2021 by Gamingninja5115

User contributions licensed under CC BY-SA 3.0