0
我一直在個人序列化過程中工作,但我堅持在保存/加載後取回解密的向量。它可以很好地保存和加載數據,但矢量庫存會在轉換中丟失它的定義,所以我必須重新制作它,這會在程序中稍後使用它。下面的代碼片段顯示了我如何加密/解密。加密/解密矢量<string>
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();
}
當我嘗試打印出character.inventory後,它崩潰。如果我沒有在character.load()函數中將character.inventory定義爲字符串,它也會崩潰。那麼我如何才能將character.inventory定義爲一個向量,我可以稍後在程序中調用(從結構體)?
我使用output_file.write((char*)&structObj, sizeof(structObj));
將數據寫入文件,並以相同的方式讀取(因此需要加密/解密,不能以這種方式保存字符串)。
請不要指示我去圖書館爲我做。
我以前從沒注意到,哎呀...但我可以(大部分)改變字符串整數就好了(幾個錯誤,我晚上在深夜做了很多這樣的代碼),我只需要能夠將character.inventory定義爲結構變量,以便稍後使用它 – asqapro 2012-08-02 12:23:53