0

Im trying to make a Banking Management System in C++. I am using file handling to parse data into program. Following is the snippet of code. I get into memory access denied issue whenever the data is parsed.

void Controller::displayCustomers()
{
    vector<Customer> custVector;
    Customer cust;
    fstream fin("customer.dat", ios::binary | ios::in);
    while (!fin.eof())
    {
        while (fin.read(reinterpret_cast<char*>(&cust), sizeof(cust)));
        {
            custVector.push_back(cust);
        }
    }
    fin.close();
    cout << "ID\tNAME\tADDRESS\tPHONE\t\tDOB\tPASSWORD\tBALANCE" << endl;
    for (vector<Customer>::iterator itr = custVector.begin();
        itr != custVector.end(); ++itr)
    {
        cout << itr->getID() << "\t" << itr->getName() << "\t" << itr->getAddress() << "\t"
            << itr->getPhone() << "\t" << itr->getDob() << "\t" << itr->getPass() <<
            "\t" << itr->getBalance() << endl;
    }
    cout << endl;
}

What should be the solution?

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • maybe the program doesn't have the permission to open it? have you checked the permission on the file? – Alberto Sinigaglia May 21 '20 at 20:25
  • 1
    What does your `Customer` class look like? – john May 21 '20 at 20:25
  • Why two while loops? That makes no sense at all. You only need one loop to read a file from beginning to end. – john May 21 '20 at 20:27
  • Where does the input file come from? How sure are you that it's compatible with your `Customer` class? – john May 21 '20 at 20:27
  • There's nothing *obviously* wrong with your code. The problem is somewhere in the answers to the comments I made above. – john May 21 '20 at 20:30
  • 3
    [Why `while(!fin.eof())` is wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Barmar May 21 '20 at 20:30
  • @Berto99 If the file open failed then the program wouldn't crash, it would just produce no output. – john May 21 '20 at 20:31
  • 2
    you can't store non-POD objects in a file. – Barmar May 21 '20 at 20:32
  • the program makes assumptions about the internal layout of cust. are they correct? – systemcpro May 21 '20 at 20:35
  • The file opens.. The data is loaded into the object, the object data is printed fine, but then the code crashes due to memory access violation. I have seen that copy constructor sometimes runs more than than the data present in file. – Ali Kamran May 21 '20 at 20:47
  • 1
    @AliKamran If your Customer class has a copy constructor then it's not suitable to be read with binarry I/O. So your program has undefined behaviour and you need to rethink your appraoch, because it cannot work as it is at present. – john May 21 '20 at 21:10
  • Whats the best solution. Im a C++ learner? – Ali Kamran May 21 '20 at 21:11
  • 1
    @AliKamran There are lots of possibilities, it would take far too long to explain them in detail, but more or less, 1) you could switch to text based I/O, 2) you could adapt your customer class so it's suitable for binary I/O, 3) you could keep your class and your binary I/O but write some code to serialize your Customer class field by field. – john May 21 '20 at 21:14
  • 1
    This may help: [https://isocpp.org/wiki/faq/serialization](https://isocpp.org/wiki/faq/serialization) – drescherjm May 21 '20 at 21:17
  • Lets say i stick to serialization, how can that be achieved? – Ali Kamran May 22 '20 at 15:36

0 Answers0