Saving and reading from a file

Hello,

Im trying to setup a sort of accounts system for a bank and im trying to save customers data into a file then on another menu choice allowing them to read this data again.

They enter their data in another part but here is where it saves using white space to space out the different data.

1
2
3
4
5
6
void Savings::saveAccount()
{
	ofstream AccFile("savings.dat", ios::app);
	AccFile << iAccNum << " " << sCusName << " "<< sCusAddress << " " << dAccBalance << " " << dInterestRate << endl;
	AccFile.close();
}


The problem comes when re opening it and reading the data, if there is a space in the address or name then it will mess up the remaining characters from showing, im guessing because it's broken up with spaces. I've tried changing these to other characters such as a comma but then it doesn't read at all.

Here is the reading in part
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
void Savings::displayDetails()
{
	fstream checkFile("savings.dat", ios::in | ios::beg);
	system("cls");

	cout << "Please enter your name:>";
	cin.ignore();
	cin.getline(name,30);

	do
	{
	   checkFile>>iAccNum>>sCusName>>sCusAddress>>dAccBalance>>dInterestRate;

	if(strcmp(name,sCusName)==0)
	{
		system("cls");
		cout << "Account Overview \n" << endl;
		cout << "Account Name: " << sCusName << "\n";
		cout << "Account Number: " << iAccNum;

		cout << endl;

		cout << "Balance: " << (char)156 << dAccBalance << endl;

		cout << "Overdraft limit: " << (char)156 << dInterestRate << endl;

		system("pause");
		break;
	}

	if(strcmp(name,sCusName)!=0)
	{
		cout << "Error name not found!" << endl;
		system("pause"); break;
	}

	}while(!(checkFile.eof()));


Any help please? :)
if there is a space in the address or name then it will mess up the remaining characters from showing,

Does this mean the space between the first name and last name like John Doe? I recommend seperating each datum with a return character and using getline().
I'm a bit of a newbie at c++ (on hindsight I should of gone to the beginners section maybe opps) would there be an easy way to implement this into the existing code, I'm not sure how getline() is used on an input from a file.
Last edited on
You can use a tab character instead of space "\t" cin will work with that as well.

If you choose a different delim like comma or pipe, then look at using something like

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

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdlib>
#include <fstream>
using namespace std;

void Trim(string &str)
{
    if ( str.empty() ) return;
    int startIndex = str.find_first_not_of(" ");
    int endIndex = str.find_last_not_of(" ");
    string temp = str;
    str.erase();
    str = temp.substr(startIndex,(endIndex - startIndex + 1));
}


vector<string> split(const string &s, const char delim)
{
    vector<string> elements;
    stringstream ss(s);
    string item;
    while (getline(ss,item,delim))
    {
        Trim(item);
        elements.push_back(item);
    }
    return elements;
}

int main()
{
    char filename[] = "testfile.txt";
    string LINE;

    ifstream inFile (filename);
    if (inFile.is_open())
    {
        while (!inFile.eof())
        {
          getline(inFile,LINE);
          vector<string> columns = split(LINE,'\t');
          for (vector<string>::iterator it = columns.begin(); it!=columns.end(); ++it) {
                cout << "  " << *it << endl;
          }
          cout << endl;
        }
        inFile.close();
    }
    else 
    {
        cout << "Error: failed to open " << filename << endl;
        exit(1);
    }
     

}


1
2
3
testfile.txt
Tom Jones	4455	1156.78	88 Pilsbury Lane	Nowhere
Harry James	1234	1234.56	123 Baker Street	Anywhere


Tom Jones
4455
1156.78
88 Pilsbury Lane
Nowhere

Harry James
1234
1234.56
123 Baker Street
Anywhere





Topic archived. No new replies allowed.