How to encrypt file text file contents
Hi!,
Well, let's imagine that I have a program which gets the age and name from console. The program will ask for the data until user presses 'q'. Data has to be within computer memory, until 'q' is pressed. And when it is pressed, then the program would write the information to the file.
Now I have to problems with this:
1) How to hold different types of variables in memory (vector's just for specific type?!)
2) How to encrypt the content of the file, yet if i would start the decrypter, it would show me the data as same way as user had entered.
Thanks,
Green Man
Re: How to encrypt file text file contents
Sounds to me like you should have a person class or struct that contains name and age members. You can then insert the objects into a vector if you like.
Code:
typedef struct Person {
std::string name;
int age;
};
...
std::vector<Person> personCollection;
Person person;
person.name = "John";
person.age = 10;
personCollection.push_back (person);
That takes care of question 1.
The encryption part of this is really up to you. You can get the ascii equivalent of a char with the atoi() function that is in the cstdlib. From there you can apply some crazy algorithm you come up with to the number and then output that. To decrypt you reverse the algorithm and cast the int back to a char.
That all
Re: How to encrypt file text file contents
Thanks, but how to write vector contents to a file?
Re: How to encrypt file text file contents
Iterate over the structure and output the contents. There are plently of examples of how to do this. In your case,
Code:
ostringstream ost;
........
std::vector<Person>::iterator iter = personCollection.begin();
for (; iter != personCollection.end(); ++iter) {
ost << "Name: " << iter->name << " Age: " << iter->age;
}
You should read up on the STL. There is a pretty good reference with examples at http://www.cplusplus.com/reference/stl/.
Regards