How do I load object data from a save file? - c++

I'm working on a project that deals with creating two strings, a username and a password. The two elements make an object of an Account. In the main, there is an Array of Accounts that is initialized at 10.
I have a Save & Quit option, which saves the Username on one line and the Password on the next in the same file. A pair of lines signifies another account.
My question is, how do you properly save the data from the Array of Accounts, then load the data from the previous Array of Accounts?
I get a std::bad_alloc memory error every time I try the loadAccounts() function. I've several different methods, but to no avail.
So far I've come up with this for saving the array (works just as it should so far) :
void saveAccounts(Account accs [], int numIndexes)
{
std::ofstream savefile("savedata.sav", std::ofstream::binary); // By re-initializing the file, the old contents are overwritten.
for (int i = 0; i < numIndexes; i++)
{
savefile << accs[i].getUsername() << endl;
savefile << accs[i].getPassword() << endl;
}
savefile.close();
}
As for my loading function I have :
Account* loadAccounts() // Load the data from the file to later print to make sure it works correctly.
{
cout << "LOADING ACCOUNTS!" << endl;
std::ifstream loadfile("savedata.sav", std::ifstream::binary);
Account * acc_arr; // The "Array" to be returned.
Account tmp_arr [10]; // The array to help the returned "Array."
acc_arr = tmp_arr; // Allowing the "Array" to be used and returned because of the actual array.
if (loadfile.is_open())
{
int i = 0;
while (loadfile.good())
{
cout << "Loadfile is good and creating Account " << i+1 << "." << endl; // For my own benefit to make sure the data being read is good and actually entering the loop.
std::string user;
std::getline(loadfile, user);
std::string pass;
std::getline(loadfile, pass);
Account tmpAcc(user, pass);
tmp_arr[i] = tmpAcc;
++i;
}
Account endAcc = Account(); // The default constructor sets Username to "NULL."
tmp_arr[i] = endAcc;
}
loadfile.close();
cout << "ACCOUNTS LOADED SUCCESSFUL!" << endl;
return acc_arr;
}
I've gathered that I can return an array by using a pointer and an actual array to do that same, since an array can't actually be returned.
I try to use the returned array here, which I'm trying to "copy" over the loaded array to the Array that will actually be printed. Later, I'll print the array (acc_arr) to ensure that the loaded array was loaded successfully :
else if (selection == 'l' || selection == 'L')
{
Account * tmp_acc_arr = new Account [10];
tmp_acc_arr = loadAccounts();
_getch();
for (size_t i = 0; i < size_t(10); i++)
{
if (tmp_acc_arr[i].getUsername() == "NULL")
{
break;
}
acc_arr[i] = tmp_acc_arr[i];
cout << "Added Account " << i << " succesfully." << endl;
}
}
The error is caused by this last block of code. I've checked to make sure the data copied correctly by using
EDIT: Awkward... by using an if statement to make sure the data within the tmp_acc_arr actually has data stored once it was returned and initialized in the main.

tmp_arr ist local in loadAccounts and on the stack. It will be invalid once loadAccounts() returns. Your return value is an invalid stack-pointer.
You could hand your pointer tmp_acc_arr to the function as an argument and fill it with the values from your file.
You should also check for overflow or better use STL containers like std::vector.
edit
void loadAccounts(Account * acc_memory, std::allocator<Account> alloc, size_t acc_array_size) // Load the data from the file to later print to make sure it works correctly.
{
Account *end_of_construction = acc_memory;
try
{
cout << "LOADING ACCOUNTS!" << endl;
std::ifstream loadfile("savedata.sav", std::ifstream::binary);
if (loadfile.is_open() && loadfile.good())
{
size_t i = 0;
for (size_t i=0; i<acc_array_size; ++i)
{
if (loadfile.good())
{
cout << "Loadfile is good and creating Account " << i+1 << "." << endl; // For my own benefit to make sure the data being read is good and actually entering the loop.
std::string user, pass;
std::getline(loadfile, user);
if (loadfile.good())
{
std::getline(loadfile, pass);
alloc.construct(end_of_construction++, user, pass);
}
else alloc.construct(end_of_construction++);
}
else alloc.construct(end_of_construction++);
}
}
loadfile.close();
cout << "ACCOUNTS LOADED SUCCESSFUL!" << endl;
}
catch (...)
{
size_t num_constructed = end_of_construction-acc_memory;
for (size_t i=0; i<num_constructed; ++i) alloc.destroy(acc_memory + i);
throw;
}
}
Used like
size_t const num_elements = 10;
std::allocator<Account> acc_alloc;
Account * tmp_acc_arr = acc_alloc.allocate(num_elements);
loadAccounts(tmp_acc_arr, acc_alloc, num_elements);
// do stuff
for (size_t i=0; i<num_elements; ++i) acc_alloc.destroy(tmp_acc_arr + i);
acc_alloc.deallocate(tmp_acc_arr, num_elements);

You return a pointer that points to an array that is destroyed as soon as you exit the function. Using that pointer leads to undefined behavior, that is BAD.
As you observed, array is not a valid return type, so to return it actually you shall put it inside a struct, and return the struct.

Related

Providing initial text for the readline buffer

In a readline buffer I want to provide an initial value the user can edit. This can either be a default value or the old value of the field already saved to my database. The entered value shall not be added to the readline-history as it is only "data" not a command.
From the samples here and on the internet I have learned that this code should do the job:
char *deftext;
int MatCustom::set_deftext ()
{
// cout << "set_deftext called ... deftext: " << deftext << endl;
if (deftext)
{
// cout << "inserting..." << endl;
rl_insert_text(deftext);
// rl_line_buffer = deftext;
// cout << rl_stuff_char('J') << endl;
rl_redisplay();
deftext = (char *)NULL;
rl_startup_hook = (rl_hook_func_t *)NULL;
// rl_pre_input_hook = (rl_hook_func_t *)NULL;
};
return 0;
}
string MatCustom::getlineWithoutHistory(const string &field) {
string returnString;
const char *buffer;
char *cstr = new char[field.length() + 1];
strcpy(cstr, field.c_str());
deftext = cstr;
// cout << "Deftext in getLineWithoutHistory: " << deftext << endl;
rl_startup_hook = set_deftext;
// rl_pre_input_hook = set_deftext;
buffer = :: readline("");
if (buffer != nullptr) {
returnString = buffer;
} else {
returnString = "";
}
free((void *) buffer);
delete [] cstr;
return returnString;
}
It works perfectly well to convert my old value to a char* and pass it to the global variable deftext, because a parameter cannot be given to the readline-hook-functions.
I am experiencing problems: Whenever I use the rl_startup_hook, which is said to be called before the prompt gets printed, I find this this hook never gets executed. However, when I use the rl_pre_input_hook, which is called prior to the input to begin, my function set_deftext actually gets called. This functions finds deftext set.
Still, I don't get any text inserted into the input buffer. The function rl_insert_text produces no change – even when I use rl_redisplay(). Directly changing the buffer with rl_line_buffer = deftext; also has no effect. When I try cout << rl_stuff_char('J') << endl; for debugging just to insert a single char, nothing happens and the function returns 0 indicating an unsuccessful insert.
Both functions are static member functions. I usually call my function with
input = MatCustom::getlineWithoutHistory(rawValue);
to edit to provided value (rawValue).

Dynamic array of structs

I have such piece of code:
typedef struct reader
{
char name[50];
char card_num[50];
char title[100];
}reader_t;
int main()
{
vector<reader> vec;
ifstream input_file("D:\\lab.txt", ios::binary);
reader_t master[1];
input_file.read((char*)&master, sizeof(master));
for (size_t idx = 0; idx < 1; idx++)
{
reader temp;
strcpy(temp.name, master[idx].name);
strcpy(temp.card_num, master[idx].card_num);
strcpy(temp.title, master[idx].title);
vec.push_back(temp);
cout << "Name: " << master[idx].name << endl;
cout << "Card num: " << master[idx].card_num << endl;
cout << "Title: " << master[idx].title<<endl;
}
cout << vec.size();
getchar();
}
What is does: it reads structures from binary file into an array of structures,copies them into vector and displays structure.And yes, I do need to do like this - I need to store structures from file in vector and this is the only working way to do it I could find(if you can tell, how to read structures to vector directly from file - you are welcome).
So,everything works fine, but the problem is that I need to create a function which would be able to do the same, but with dynamic array.I wrote something like this:
void read_structs(int vec_size)
{
ifstream input_file("D:\\lab.txt", ios::binary);
//Here I commented 2 ways how I tried to create a dynamic array of structs
//reader* master = new reader[vec_size];
//reader* master = (reader*)malloc(sizeof(reader) * vec_size);
input_file.read((char*)&master, sizeof(master));
for (size_t idx = 0; idx < vec_size; idx++)
{
reader temp;
strcpy(temp.name, master[idx].name);
strcpy(temp.card_num, master[idx].card_num);
strcpy(temp.title, master[idx].title);
vec.push_back(temp);
cout << "Name: " << master[idx].name << endl;
cout << "Card num: " << master[idx].card_num << endl;
cout << "Title: " << master[idx].title<<endl;
}
}
And that worked fine too unless I tried to run it.VS wasn't higlighting error in my code, it just was throwing an exception right as the moment when the program tried to access master[0].name.
There is absolutely no point in the temp struct. See, the
vec.push_back(temp);
is already using copy constructor, so copy constructor must work and then the set of strcpy is not doing anything different from that, so just go with
vec.push_back(master[0]).
You can't read into vector directly. You do need to read into temporary. So that is correct. Except I suppose you want to read all entries from the file no matter how many of them there are, so you need to put the read itself also into the loop.
There is not much point in creating an array of one element.
reader_t master[1];
input_file.read((char*)master, sizeof(master));
// ^ you *don't* need & here, arrays degrade to pointers automatically
and
reader_t master;
input_file.read((char *)&master, sizeof(master));
// ^ but you do need & here.
are equivalent. I would go with the later.
So we are basically down to:
reader temp; // calling it temp; the master name makes no sense.
while (input_file.read((char*)&temp, sizeof(temp)))
// read returns input_file and input_file is false if last operation failed
{
vec.push_back(temp);
// verify the stored values by reading back vfrom vec.back().
cout << "Name: " << vec.back().name << endl;
cout << "Card num: " << vec.back().card_num << endl;
cout << "Title: " << vec.back().title<<endl;
}
In the second example, you didn't initialize master, so it obviously crashed.
There is a more C++ approach though. First, you define a read operator for the structure:
std::istream &operator>>(std::istream &in, reader &r) {
return in.read((char *)&r, sizeof(r));
}
and then you simply read the vector using the istream_iterator:
vec.assign(std::istream_iterator<reader>(input_file),
std::istream_iterator<reader>());
and the standard library will generate the above loop for you.

Vector of pointers pointing to object, can't access object data fields. (expression must have pointer type)

I have created my own Vector and Array classes which I have tested and work fine. However, in the following code I am having problems accessing the methods in an AccountInfo class.
_accounts is declared like this:
Vector<AccountInfo*> _accounts[200]; // store up to 200 accounts
Before, _accounts was only of type AccountInfo. After switching to a vector, every instance that uses _accounts has the error: Expression must have pointer type.
Here is my code:
ostream& operator << (ostream& s, UserDB& A){
// print users
for (unsigned int i = 0; i < A._size; i++){
//print name
s << A._accounts[i]->getName() << endl;
}
return s; // return the statement
}
//---------------------------------------------------------------------------- --------------------
//Destructor of UserDB
UserDB::~UserDB(){
for (int i = 0; i < _size; i++){
delete[] _accounts[i]; // delete objects in _accounts
}
}
//------------------------------------------------------------------------------------------------
// add a new user to _accounts
void UserDB::adduser(AccountInfo* newUser){
_accounts[_size] = newUser;
_size++; //increment _size.
if (newUser->getUID() > 0){
//print if UID has a value
cout << newUser->getName() << " with " << newUser->getUID() << " is added." << endl;
}
else {
newUser->setUID(_nextUid); //automatically set UID for user if empty
cout << newUser->getName() << " with " << newUser->getUID() << " is added." << endl;
_nextUid++;
}
}
Is there a way to access the AccountInfo methods from the variable _accounts?
You defined an array of vectors with:
Vector<AccountInfo*> _accounts[200]; // store up to 200 accounts
This is not the same as declaring a single vector with a capcity of 200. If you were using a standard vector then it would look like:
std::vector<AccountInfo*> _accounts(200); // store up to 200 accounts

Issue in reading repeated fields from protocol buffers in C++

org.proto
message Optimize {
required int element_size = 1;
required string element_name = 2;
}
message nodes {
repeated Optimize = 1;
}
Have this decoding function:
DecodeNodeMsg(char *msg, int size)
{
Org::nodes node;
int element_size;
string element_name;
int NumofElem = 0;
node.ParseFromArray((void *)msg, size);
for (int i = 0; i < nodes.Optimize().size(); i++)
{
element_size = nodes.Optimize(i).element_size();
element_name = nodes.Optimize(i).element_name();
cout << "size" << element_size << endl;
cout << "name" << element_name << endl;
NumofElem++;
}
cout << "number of" << NumofElem << endl;
}
I am encoding a nodes message with three Optimize messages in it. and calling this decode function. Encoding part is an old code which is working fine from long time. So i dont suspect the encoding function.
In the decode function, I see that the NumofElem is correctly printed as three. However, i see that both element_size & element_name are just garbage. Integer has some junk value and string has a binary data.
I am having this issue only if this repeated fields. If fields are required/optional fields, then i dont have this issue.
Could some one has similar issue... ? if so any clues on how to fix this ?
Thanks,
Kiran
I don't see where you are actually decoding a message. I see you creating a new node object, but then calling Org::nodes() which looks wrong. I think you need to access the Optimize elements like this:
for (int i = 0; i < node->optimize_size(); i++)
{
element_size = node->optimize(i).element_size();
element_name = node->optimize(i).element_name();
cout << "size" << element_size << endl;
cout << "name" << element_name << endl;
NumofElem++;
}
But again I think your nodes object needs to be decoded from something. The mutable methods allow you to set data. There's also the ParseFrom methods. Also in my proto files I number the elements in a message. See https://developers.google.com/protocol-buffers/docs/overview
message nodes {
repeated Optimize = 1;
}
The function deserializes the buffer into the local variable node, but the loop references nodes. I'd also validate the return value from ParseFromArray.

Access violation help C++

I am currently getting this error in my code: Unhandled exception at 0x0FF321E8 (msvcp110d.dll) in bankAccountp5.exe: 0xC0000005: Access violation writing location 0xCCCCCCF8. And I'm certain it's to do with an array of objects I have created.
Here is part of my code:
class bankAccount: public bankBranch
{
public:
bankAccount(int, int, int, string, int);
int setAccountNumber ()
{
bankAccountNumber = ++minAccountNumber;
return this->bankAccountNumber;
}
void setAccountBalance ()
{
for(i = 0; i < getNumOfBankAccounts(); i++)
{
cout << "Enter the balance for your bank Account: " << endl;
cin >> accounts[i]->bankAccountBalance;
if (bankAccountBalance > MAX_BALANCE || bankAccountBalance < MIN_BALANCE)
{
cout << "Incorrect bank balance, please try again!" << endl;
--i;
} else {
++i;
}
}
}
void menuSystem(int choice) {
}
void displayBankBranchDetails()
{
cout << "\n";
cout << "DETAILS OF YOUR BANK BRANCH" << endl;
cout << "BSB Number: " << this->getBsbNumber() << endl;
cout << "Address: " << this->getAddress() << endl;
cout << "Postal code: " << this->getPostCode() << endl;
}
void setBankAccountDetails() {
}
int getNumOfBankAccounts() {
return this->numOfBankAccounts;
}
void createBankAccounts()
{
valid = false;
while (valid == false) {
cout << "How many Bank Accounts would you like to create under the Bank Branch BSB: " << getBsbNumber() << endl;
cin >> numOfBankAccounts;
if ( numOfBankAccounts <= MAX_NUMBER_ACCOUNTS)
{
valid = true;
} else {
valid = false;
}
}
}
private:
//bankAccount Data
int bankAccountNumber;
int numOfBankAccounts;
int bankAccountBalance;
int interestRate;
//constants
const static int MAX_NUMBER_ACCOUNTS = 10;
const static int MAX_BALANCE = 100000;
const static int MIN_BALANCE = 0;
//objects
bankBranch homeBranch;
bankAccount* accounts[MAX_NUM_ACCOUNTS];
//misc
int minAccountNumber;
int i;
bool valid;
};
The error occurs when I get to void setAccountBalance(), and I call the array of objects accounts, could anyone help me out with this? Cheers.
You have declared an array of pointers, you have to assign memory to it dynamically, bankAccount* accounts[MAX_NUM_ACCOUNTS];
moreover you don't need to write the else part in setAccountBalance()
This
bankAccount* accounts[MAX_NUM_ACCOUNTS];
creates an array of pointers. You need to take the next step of actually allocating memory for each account. Something like
accounts[some-index] = new bankAccount();
accounts = new bankAccount[MAX_NUM_ACCOUNTS]; needs to be done in one of your functions. You have declared a dynamic array, but still need to initialize it.
EDIT: You should really consider using stl structure vector. This will allow you to just push new accounts into your array and a variety of other things. Using pointers and dynamically allocated arrays need you to manage your memory and other such unnecessary pains.
Check this out: http://www.cplusplus.com/reference/vector/vector/
Add using std::vector after your inclusions. This way you don't need to keep typing std::vector, you can just say vector.
Then, vector<bankAccount> accounts will declare a vector called accounts. When adding new accounts, you can just call accounts.push_back(_______). You can access elements with [] and you also have accounts.size().
As #scottwilson said. Instead use a std::vector<bankAccount> that contain statically allocated bankAccounts.
Else, you might have to allocate memory for each bankAccount pointer, either statically or as you might require, dynamically.
You also need a createBankAccount() function that will allocate this for you whenever you want another bankAccount object:
like so:
public:
void createBankAccount() {
accounts[getNumberOfBankAccounts()] = new bankAccount();
numOfBankAccounts++;
}
Call this function each time you need a new bank account.