Writing to binary does not work

The code below compiles and runs successfully, but on exit, but nothing is written to the file. the file is created, when choosing a register, it requests all the data and shows the successful registration of the message. FILE customers.dataIS EMPTY (file size - 0kb). The function writedoes not write the object to a file. All things seem correct in the code. Unable to determine the error. Please, help

main program:

int main()
{
    int ch;
    Customer cust;
    fstream file;
    file.open("customers.data",ios::out|ios::app|ios::binary);
    if(!file)
    {
        cout<<"\nError Files are missing. Unable to create files\n";
        system("pause");
        exit(1);
    }
    cout << "WELCOME! Press any key to continue.\n\n";
    system("pause");
    do
    {
        system("cls");
        cout<<"\n\n\n";
        cout<<"1->Login"<<endl;
        cout<<"2->Register"<<endl;
        cout<<"3->Exit"<<endl;
        cout<<"Choose An option: ";
        cin>>ch;
        switch(ch)
        {
        case 1:
            if(cust.Login()==true)
            {
                LaunchCustomerMenu();
            }
            else
            {
                cout<<"Incorrect Email/Password\n";
                system("pause");
            }

            break;
       case 2:
        cust.Register();
        file.write((char*)&cust, sizeof(cust)); //does not work
        if(!file.fail()) //still true
        {
            cout<<"\n\nRegistration Successful"<<endl;
            system("pause");
        }
         else
            {
                cout<<"\nTheir was a Error processing your Registration\n";
                system("pause");
            }

            break;
        case 3:
            exit(0);
            break;
        default:
            cout<<"\n\nWRONG OPTION\n";
            cout<<"Choose Again\n\n";
            system("pause");
        }
        cin.ignore();
    }
    while(ch!=3);
    file.close();
    return 0;
}

class:

class Customer
{
private:
    unsigned int CustomerID;
    string CustomerName;
    string CustomerAddress;
    string CustomerEmail;
    string CustomerPassword;
    unsigned int CustomerPhone;
public:
    Customer();

    void Register();
    bool Login();

    unsigned int getID();
    string getName();
    string getEmail();
    string readPassword();
    unsigned int getPhone();
    unsigned int RandomID();

    void modifyName();
    void modifyAddress();
    void modifyEmail();
    void modifyPassword();
    void modifyPhone();
};

CustomerID randomly generated.

function register:

void Customer::Register()
{
    cout<<"Enter Your Name: ";
    cin.ignore();
    getline(cin,CustomerName);
    cout<<"Enter Your Address: ";
    cin.ignore();
    getline(cin,CustomerAddress);
    cout<<"Enter Your Email: ";
    cin>>CustomerEmail;
    cout<<"Enter A Password: ";
    cin.ignore();
    getline(cin,CustomerPassword);
    cout<<"Enter Your Phone no. :";
    cin>>CustomerPhone;
}

EDIT: In accordance with the answer below, I added a check for file.fail(), but it returns false and customers.dataempty. Ie, the writeoperation cannot write the object.

+4
source share
2

, .

char, .

, . - , (SIGSEGV). , , . SIGSEGV, .

+2

fstream write , 3 true. good, bad fail, , .

, , , (, ..). . .

+4

Source: https://habr.com/ru/post/1612677/


All Articles