-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();
}
  • This will not work. You cannot save non-trivially-copyable types like this. The `Files` has `std::string` members, a user-defined destructor, etc. Look up [object serialization](https://stackoverflow.com/questions/523872/how-do-you-serialize-an-object-in-c). This cannot work if you look closely at `sizeof(Files)`. What if one of those strings represents 1000 characters? Do you think that `sizeof(Files)` will be 1000 or greater? No. – PaulMcKenzie Apr 16 '21 at 00:31
  • Also, who or what suggested to read / write binary files? I see this error so many times on SO, someone or some material is presenting this as a viable method. Yes, it is viable, but only if the types being stored are trivially-copyable and contains no pointers. – PaulMcKenzie Apr 16 '21 at 00:36
  • Unrelated fun fact: `struct` defaults to `public`. No need to declare everything `public` – user4581301 Apr 16 '21 at 00:42
  • 1
    I see that you are reading a char array. But looking at your code, it looks like you took some (wrong) code that erroneously tried to read/write `File` objects, and totally forgot to remove the `sizeof(Files)` and replace it with `sizeof(password)`. – PaulMcKenzie Apr 16 '21 at 00:54
  • Yeah that fixed it, Thanks. changing `sizeof(Files)` to `sizeof(username)` and `sizeof(password)` worked – AdhDyslexie Apr 16 '21 at 01:02

1 Answers1

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

  • I am glad you have got your solution and thanks for your sharing, I would appreciate it if you mark them as answer and this will be beneficial to other community. – Jeaninez - MSFT Apr 16 '21 at 01:51