I've been working on a personal serializing process, but I'm stuck at getting back the decrypted vector after saving/loading. It saves and loads the data just fine, but the vector inventory loses it's definition in the transition, so I have to remake it, which removes the ability to use it later in the program. The below snippet of code shows how I encrypt/ decrypt.
struct bin{
map<string, int>::iterator it;
int modify_string;
vector<string> uncrypt_vec;
map<string, int> encryption;
void set_up_code(){
string alphabet[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
int letter_num=1;
for(int i = 0; i < 27; ++i){
string letter = alphabet[i]; //Assign each letter to a number
encryption.insert(pair<string,int>(letter,letter_num));
letter_num++;
}
encryption.insert(pair<string,int>(" ", -1));
}
int encrypt_vector(vector<string>& crypt){
stringstream ssv;
for(unsigned int i = 0; i < crypt.size(); ++i){
string word = crypt[i]; //Grab each string
int mod_word[word.size()];
for(unsigned int x = 0; x < word.size(); ++x){
stringstream ssl;
string letter;
char let = word[x]; //Convert each letter of the string into an integer
ssl << let;
ssl >> letter;
int num = encryption[letter];
mod_word[x] = num;
}
mod_word[word.size()] = 0; //Include a zero for each word
copy(mod_word, mod_word+word.size()+1, ostream_iterator<int>(ssv));
}
ssv >> modify_string; //Put into int modify_string and return
return modify_string;
}
vector<string> decrypt_vector(int& uncrypt){
string num;
stringstream ssn;
ssn << uncrypt; //Store the numbers in a string
ssn >> num;
string mod_word;
string word;
for(unsigned int i=0;i<num.size();++i){
int number;
char check_num = num[i];
stringstream ssctn;
ssctn << check_num; //Kinda stupid way of doing it
ssctn >> number; //Pulls int from num[i], then checks it against 0
if(number==0){ //If break point
uncrypt_vec.push_back(mod_word); //Push back the word
mod_word.clear();
}
int letter;
stringstream sscn;
char let = num[i];
sscn << let; //Pull each letter from uncrypt, change back to letter based on encryption
sscn >> letter;
for(it = encryption.begin(); it != encryption.end(); ++it){
if(it->second == letter){
mod_word.append(it->first);
break;
}
}
}
cout << "Done" << endl;
return uncrypt_vec;
}
}binary;
struct player{
vector<string> inventory;
int inventory_en; //en for encrypted
void save(){
inventory_en = binary.encrypt_vector(inventory);
}
void load(){
vector<string> inventory; //Must recreate, type is lost otherwise, crashes
inventory = binary.decrypt_vector(inventory_en);
vector<string>::iterator i;
for(i=inventory.begin();i!=inventory.end();++i){
cout << *i << endl; //Prints correctly here
}
}
}character;
int main(){
character.inventory.push_back("abc");
character.inventory.push_back("edf");
binary.set_up_code();
character.save();
character.load();
}
When I try to print out character.inventory later, it crashes. And if I don't define character.inventory as a string in the character.load() function, it also crashes. So how can I get character.inventory defined as a vector that I can call (from the struct) later in the program?
I write data to files using output_file.write((char*)&structObj, sizeof(structObj)); and read out the same way (hence the need to encrypt/ decrypt, can't save strings this way).
And please don't direct me to a library to do it for me.