I/O problems including a map container

Hello,

I am having a hard time understanding what the problem with my code is. The problem that occurs is coming from the I/O I believe. I am creating a bank handling program and I am storing the accounts in a <map> and then I am storing the accounts in a txt file.

The problem I have is when ever I am closing the application and restarting it and want to read the txt file I am getting nothing or I am getting really odd results. In the application it can be that it does not read anything or it could be that I get
Acc nbr: 0
Name: -47000300
Type: 12333993
balance: Å

it doesnt make sense. But in my txt file its perfectly normal. Sometimes when Ive been trying to play around with the code the txt file changes the text to kanji signs.

I've created an own implemented class that handles the account for example, deposition, overloading constructor of creating an account, withdraw, returning various information. The attributes are all public.

the read file function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void read_file(){
    string name;
    int pnbr;
    int accNbr;
    int balance;
    char type;
    ifstream infile;
    infile.open("list.txt");
    if(infile.is_open()){
        while(true){
            infile >> name;
            infile >> pnbr;
            infile >> balance;
            infile >> type;
            infile >> accNbr;
            Accounts acc(name, pnbr, balance, type, accNbr);

            if(infile.eof()){
                break;
            }
            
            mymap.insert(pair<int, Accounts>(accNbr, acc));
           //mymap.insert(make_pair(accNbr, acc));

        }

    }
    else{
        cerr << "File could not be opened! " << endl;
        exit(EXIT_FAILURE);
    }

    infile.close();
}


The write to file function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void write_file(){ //Writes to a file
    ofstream outFile;
    outFile.open("list.txt", ios::out | ios::app);
    if(outFile.is_open()){
        for(it = mymap.begin(); it != mymap.end(); it++){
            outFile << it->second.getName()<<" " << it->second.getPnbr()<<" "
 << it->second.getBalance()<<" "  << it->second.getAccountType() <<" "
 << it->second.getAccountNbr();
        }
        outFile.close();
    }
    else{
        cout << "Could not open file" << endl;
    }

}


I am going to post out the print function that is only internal for the map.

1
2
3
4
5
6
7
8
9
10
11
void print(){ //Prints out all information that is stored in the map
    for(it = mymap.begin(); it != mymap.end(); it++){
        if(mymap.size()==0){
            cout << "There is no accounts stored" <<endl;
        }
        else{
            cout<< it->second.returnInformation()<< endl;

        }
    }
}
1
2
3
outFile << it->second.getName()<<" " << it->second.getPnbr()<<" "
 << it->second.getBalance()<<" "  << it->second.getAccountType() <<" "
 << it->second.getAccountNbr();
What will separate first account number and next account name?
Last edited on
I am sorry but i did not understand your question. Can you please rephrase it? @MiiNiPaa
How would your output file look after you write two entries in it? No need to actually run program, just compose file manually looking at what your program does.
Sorry the outFile should look like this:

1
2
3
outFile << it->second.getName()<<" " << it->second.getPnbr()<<" "
 << it->second.getBalance()<<" "  << it->second.getAccountType() <<" "
 << it->second.getAccountNbr()<<endl;

Then in the file it would look like this
1
2
John 890115 10000 S 12
Wayne 790214 20000 C 13
Show yours returnInformation function.
And make a minimal case reproducing your problem: a one compiling file containing code just enough to reproduce the problem.
1
2
3
4
5
6
string Accounts::returnInformation(){
    stringstream s;
    s << "Account Holder: " << getName()<< endl << "SS ID: " << getPnbr() <<endl<< "Balance: " << getBalance()<<endl << "Type of account: " << getAccountType() << endl<< "Account: "<< getAccountNbr()  <<endl;
    return s.str();

}


if im understanding correctly of the reporducable code this should be enough then?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <istream>
#include <map>
#include <algorithm>
#include <utility>
#include <string>
#include <sstream>
using namespace std;

Accounts account;
map<int, Accounts>mymap;
map<int, Accounts>::iterator it;

void read_file(){
    string name;
    int pnbr;
    int accNbr;
    int balance;
    char type;
    ifstream infile;
    infile.open("list.txt");
    if(infile.is_open()){
        while(true){
            infile >> name;
            infile >> pnbr;
            infile >> balance;
            infile >> type;
            infile >> accNbr;
            Accounts acc(name, pnbr, balance, type, accNbr);

            if(infile.eof()){
                break;
            }

            mymap.insert(pair<int, Accounts>(accNbr, acc));


        }

    }
    else{
        cerr << "File could not be opened! " << endl;
        exit(EXIT_FAILURE);
    }

    infile.close();
}


int main()
{
        read_file();
        bool quit = false;
        int choice = 0;
        while(quit == false){
            cout<<"Please choose an option by typing a number" << endl;
            cout << "2. Print "<<endl;
            cin>>choice;

                    case 2:
                        choice = 2;
                        print();
                    break;

                default:
                break;
            }
        }
}
Last edited on
You din not post either returnInformation() function, nor minimal reproducable code.
Sorry I misread. I've edited the code above.
Last edited on
if im understanding correctly of the reporducable code this should be enough then?
Copy-paste it and try to compile. If it is not compiling, it is not enough.

Your display function looks fine, so it might be problems with: (1) Accounts constructor, (2) get* functions, (3) something else.
Topic archived. No new replies allowed.