Thanks for that. I suspect it may need a bit of fine-tuning. Some of the fields may need increasing in length, remember a c-string needs an extra byte for the terminating null character. Thus acctype[2] can store a string of length 1 character.
For now, you could save a baccount to file a bit like this:
1 2 3 4 5 6
|
const char delim = '\t';
std::ofstream fout("accounts.txt");
fout << acct.accno << delim << acct.acctype << delim
<< acct.fname << delim << acct.lname << delim
<< acct.amt << '\n';
|
That will write all the items on a single line separated by the tab character used as a delimiter.
Reading the data back in, I'd prefer to read the whole line at a time and then split it using a stringstream, but it still amounts to almost the same thing, use getline with the same delimiter,
|
file_in.getline(acct.accno, delim);
|
and so on.
There are always additional considerations, what to do if the data is longer than the string in which it is to be stored, how to handle the last field on the line, any trailing whitespace etc.
Another approach is to use a file in binary format and read or write the entire structure in a single operation. I'd leave that idea until you have a bit more experience with files, I don't recommend it as a starting point.