Click here to Skip to main content
15,917,610 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
In below mentioned code of function. I am not able to update balance(deposits and withdrawals) in file my.dat. Please advise where am I doing it wrong. (I am a newbie).


C++
void dep_with(int e, int f)
{
    int amt;
    int recordFind=0;
    account ac;
    ifstream updatedata("E:\\c++ Project\\final thoughts\\my.dat", ios::in|ios::out);
    while(updatedata.read((char*) &ac, sizeof(ac)) && recordFind==0)
    {
        if(ac.get_account()==e)
        {
            ac.view_account();
            if(f==1)
            {
                cout<<"\nEnter the amount to be deposited";
                cin>>amt;
                ac.deposits(amt);
            }
            if(f==2)
            {
                cout<<"\nEnter the amount to be withdraw";
                cin>>amt;
                ac.withdrawls(amt);
            }
            int pos=(-1)*sizeof(ac);
            ofstream updatedata("E:\\c++ Project\\final thoughts\\my.dat", ios::in|ios::out|ios::app);
            updatedata.seekp(pos,ios::cur);
            updatedata.write((char*) &ac, sizeof(ac));
            cout<<"\n\n\tRecord Updated";
            recordFind=1;
        }

    }
    updatedata.close();
    if(recordFind==0)
    {
        cout<<"\n\nRecord not Found";
    }
}
Posted
Updated 19-Oct-13 21:48pm
v2
Comments
[no name] 20-Oct-13 6:20am    
I'm, not familar with ifstream and ofstream, thatswhy only a comment and not a solution.
For me the problem seems to be your ofstream . You seek there back by -1*sizeof(ac). But ofstream you just opened and at the beginning of the file....why not updating directly by the ifstream?
Member 10347014 20-Oct-13 6:29am    
uodatedata.write cannot be used with ifsteam it can be used with ofstream and to update i need updatedata.write. I dont know may be i am wrong but that updating thing directly by the ifsteam is not working ...
[no name] 20-Oct-13 6:39am    
If it is like this, than you simply have too seek to the right position in ofstream. And that means not -1 back, you have to count while reading and than seek to (count - 1) * sizeof(ac) in ofstream.

And please see solution one. That with the naming i did not realize...nv3 comment also about this is very correct.

1 solution

For one, it's no good idea to work with two separate streams for reading and writing. And secondly, it is an especially bad idea to name them both the same (updatedata). When doing the seekp you are referring to the position of the inner updatedata stream, which is still positioned at the beginning. Hence you will be writing to a negative position, which will probably go wrong.

[AMENDED]
Why don't you use fstream instead of ifstream. Then you can do the reading and writing with the same stream.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900