-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileHandler.cpp
56 lines (46 loc) · 1.84 KB
/
FileHandler.cpp
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
#include "FileHandler.h"
void FileHandler::saveDataToFile(const BankAccount accounts[], int accountCount) {
std::ofstream outputFile("bank_data.csv");
if (!outputFile.is_open()) {
std::cerr << "Error opening file for writing!\n";
return;
}
for (int i = 0; i < accountCount; ++i) {
outputFile << std::setw(20) << accounts[i].Name << ","
<< std::setw(30) << accounts[i].LName << ","
<< std::setw(30) << accounts[i].PhoneNum << ","
<< std::setw(30) << accounts[i].City << ","
<< std::setw(100) << accounts[i].Address << ","
<< accounts[i].deposit << ","
<< accounts[i].Pass << ","
<< accounts[i].AccountNum << "\n";
}
outputFile.close();
}
void FileHandler::loadDataFromFile(BankAccount accounts[], int& accountCount) {
std::ifstream inputFile("bank_data.csv");
if (!inputFile.is_open()) {
std::cout << "No existing data file found.\n";
return;
}
std::string line;
while (std::getline(inputFile, line)) {
std::istringstream iss(line);
BankAccount account;
// Assuming the CSV file format: Name,Last Name,Phone,City,Address,Deposit,Password,AccountNum
if (!(iss >> std::setw(20) >> account.Name
>> std::setw(30) >> account.LName
>> std::setw(30) >> account.PhoneNum
>> std::setw(30) >> account.City
>> std::setw(100) >> account.Address
>> account.deposit
>> account.Pass
>> account.AccountNum)) {
std::cerr << "Error reading data from file.\n";
break;
}
// Add the loaded account to the array
accounts[accountCount++] = account;
}
inputFile.close();
}