Map and ofstream/ifstream

closed account (218pE3v7)
Hey guys
So, I what I am trying to accomplish is I have a account class and a bank class. my bank class uses a map which stores the account object. upon starting the program the bank class will read the file from a txt and store them into my map. i have a static variable which keeps track of the account number which starts as 1. so the problem is when i start my program the initial txt file is empty. however my map is storing a 0. the bottom is my constructor for bank class.
1
2
3
4
5
6
7
8
9
10
bank::bank(){
    std::ifstream myfile("bank.txt");
    account a;
    while(!myfile.eof()){
        myfile>>a;
        mybank.insert(std::make_pair(a.getaccountnumber(),a));
    }
    a.setaccountnumbercounter(a.getaccountnumber());
    myfile.close();
}


my destrutor will read my map and store the data into my txt file
1
2
3
4
5
6
7
8
bank::~bank(){
    std::ofstream myfile("bank.txt");
    std::map<long,account>::iterator itr=mybank.begin();
    for(;itr!=mybank.end();itr++){
        myfile<<itr->second;
    }
    myfile.close();
}
Last edited on
It's pretty much impossible to tell where the zero is coming from, from the code you've posted.

You must have something that displays all account details. Does that show an account zero?
closed account (218pE3v7)
yes it does upon running the code using a empty txt file. exiting the code right away should not have input anything back into the txt. but an account 0 was shown on the txt
a filestream's eof is set only AFTER you try to read past the end of the file. Opening up an empty file the eof flag is not set initially. Line 5 you try to read a from the file without testing you've actually read any valid data. If the data is bad you then add that bogus data into mybank. You loop back to the beginning of your while and only then after the bad read is the file's eof set.

https://stackoverflow.com/questions/4533063/how-does-ifstreams-eof-work

You might want to modify the while statement's logic to read the data and then check for eof status before you add the read data to mybank.

Without testing, I am not at my development PC at the moment, move your read at line 5 into the while at line 4:
while(myfile >> a && !myfile.eof())

If others find this wrong I am sure they will be more than willing to say so. :Þ
Last edited on
my destrutor will read my map and store the data into my txt file

It's conceptually difficult to do substantive work in a destructor because destructors, conceptually, may not fail. Substantive work, however, generally can fail.

This is a major issue if your software was actually managing my money.

You might be better off with an explicit commit function which must be called to make any irreversible change. For instance, make changes into a temporary buffer, and write bank.txt only upon explicit request.

Without testing, I am not at my development PC at the moment, move your read at line 5 into the while at line 4:
while(myfile >> a && !myfile.eof())


You don't need the &&.... Just:

 
while (myfile >> a)


Not withstanding mbozzi comment re the destructor,

1
2
3
4
5
6
7
8
bank::~bank(){
    std::ofstream myfile("bank.txt");
    std::map<long,account>::iterator itr=mybank.begin();
    for(;itr!=mybank.end();itr++){
        myfile<<itr->second;
    }
    myfile.close();
}


can be simplified:

1
2
3
4
5
6
7
8
bank::~bank(){
    std::ofstream myfile("bank.txt");

    for(auto itr = mybank.begin(); itr != mybank.end(); ++itr)
        myfile << itr->second;

    myfile.close();
}


or even:

1
2
3
4
5
6
7
8
bank::~bank(){
    std::ofstream myfile("bank.txt");

    for (const auto& [no, ac] : mybank)
        myfile << ac;

    myfile.close();
}

Last edited on
Topic archived. No new replies allowed.