4

The code below compiles and run successfully but on exit but nothing gets written to the file. the file gets created, on opting register it asks all details and shows the message registration successful.THE FILE customers.data IS EMPTY(file Size is 0kb). The function write does not write the object to the file.All things seems correct in the code.Can't identify the bug. Please Help

the 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;
}

the 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 is randomly generated.

the 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: According to an answer below I added a check for file.fail() but it returns false and customers.data is empty i.e, write operation fails to write the object.

3
  • Smells like you should be using an existing database rather than inventing your own. Commented Oct 22, 2015 at 4:25
  • @ThomasMatthews well I have written the code from scratch. Commented Oct 22, 2015 at 4:37
  • If this is homework, you have no choice. Otherwise, databases have all this written and debugged; which is why you should use a database. Commented Oct 22, 2015 at 17:17

2 Answers 2

4

The fstream write call returns a reference to itself, so the if check for option 3 is always going to return true. You need to call one of the good, bad or fail functions to check if it actually succeeded.

Also, it's generally a bad idea to write the actual contents of an object to, well, anywhere (socket, disk, etc.). You should look up the concept of serialization. That's the proper way to write class data to disk.

Sign up to request clarification or add additional context in comments.

2 Comments

its for my school assignment and I cant go beyond what was taught so I had to write whole object data to file.
@tango If those are your constraints, then fine. But keep that in mind for the future. If you aren't aware, the compiler will usually put a extra data into the actual memory of an object (like the virtual table, padding, etc.). So when you write the object to disk, you're writing that as well. This is hell on portability and maintainability and a bunch of other -ilities.
3

The same occurred with me and I managed to find the error.

It works fine when using traditional char variable type to declare an array to hold the string values.

Their seems to be some issue with implementation of the string library. Even if you are somehow able to write the values to the file with string datatype on reading the values generates a run time error(SIGSEGV) . I think the string uses pointers for dynamic variable allocation not suitable for use with files. That generates SIGSEGV as allocated variable space no longer remains in memory after running the program.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.