Writing to a .dat file

I am trying to write a list to a .dat file, something like:

12; John Smith; 1000
35; Adam Smith; 1525

But when I loop my code and input more than one "set" of information, it overwrites the previous one and only shows the most recent "set" of information that I input.

What are some ideas I can work with to adjust my code to input in a list-like fashion?

Thank you!

And looking forward, I'm trying to read through the list and stop the user from entering duplicates.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct accountStruct
{
	int account_number;
	string name_owner;
	double amount_avail;
};

int main()
{
	accountStruct new_account;

	cout << "Enter desired account number. " << endl;
	cin >> new_account.account_number;

	cout << "Enter first name. " << endl;
	string firstname;
	cin >> firstname;

	cout << "Enter last name. " << endl;
	string lastname;
	cin >> lastname;

	new_account.name_owner = firstname + " " + lastname;

	cout << "Enter initial amount available. " << endl;
	cin >> new_account.amount_avail;

	ofstream myfile;
	myfile.open("Accounts.dat");
	myfile << new_account.account_number << "; " << new_account.name_owner << "; " << new_account.amount_avail;
	myfile.close();

}
Make sure your ofstream functions don't return errors.
By default ofstreams truncate file, you need to open file in append mode
http://en.cppreference.com/w/cpp/io/basic_ofstream/open
Topic archived. No new replies allowed.