#include <iostream>
#include <fstream>
#include <Windows.h>
#include <filesystem>
enum : uint16_t { ECustomer, EAdmin };
typedef struct _ACCOUNT_DATA {
// Fields
const std::string m_sLogin, m_sPassword;
uint16_t m_uiPrivilege;
// Constructors
_ACCOUNT_DATA( const std::string lsLogin, const std::string lsPassword, const uint16_t luiPrivilege ) : m_sLogin( lsLogin ), m_sPassword( lsPassword ), m_uiPrivilege( luiPrivilege ) {}
} ACCOUNT_DATA, *PACCOUNT_DATA;
std::vector<ACCOUNT_DATA> &AddAccount( std::string lsLogin, std::string lsPassword, uint16_t luiPrivilege ) {
static std::vector<ACCOUNT_DATA> g_vAccounts = std::vector<ACCOUNT_DATA>();
ACCOUNT_DATA account = ACCOUNT_DATA( lsLogin, lsPassword, luiPrivilege );
g_vAccounts.push_back( account );
return g_vAccounts;
}
std::ofstream &operator<<( std::ofstream &ofs, const _ACCOUNT_DATA &p ) {
ofs << p.m_sLogin << "|" << p.m_sPassword << "|" << p.m_uiPrivilege;
return ofs;
}
BOOL SaveAccounts( std::vector<ACCOUNT_DATA> lvAccounts ) {
std::ofstream file( "accounts.data", std::ios::out | std::ios::trunc );
if ( !file.is_open() ) {
std::cout << __FUNCTION__ << ": Failed to open file." << std::endl;
return false;
}
if ( lvAccounts.size() > 1 )
for ( size_t i = 0; i < lvAccounts.size() - 1; i++ )
file << lvAccounts[i] << std::endl;
file << lvAccounts[lvAccounts.size() - 1];
file.close();
return true;
}
int main() {
// Russian encoding
SetConsoleCP( 1251 );
SetConsoleOutputCP( 1251 );
std::string sLogin, sPassword;
std::cout << "Write your login: ";
std::cin >> sLogin;
std::cout << "Write your password: ";
std::cin >> sPassword;
// Test
AddAccount( "Example", "Vector", ECustomer );
AddAccount( "Im", "User", EAdmin );
AddAccount( "Log", "Pass", ECustomer );
auto vAccounts = AddAccount( sLogin, sPassword, EAdmin );
SaveAccounts( vAccounts );
// Pause app
system( "pause" );
return 0;
}