CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2007
    Posts
    7

    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

  2. #2
    Join Date
    May 2007
    Posts
    13

    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
    Last edited by crei; May 5th, 2007 at 03:24 PM.

  3. #3
    Join Date
    Apr 2007
    Posts
    7

    Re: How to encrypt file text file contents

    Thanks, but how to write vector contents to a file?
    Last edited by Green Man; May 6th, 2007 at 02:41 AM.

  4. #4
    Join Date
    May 2007
    Posts
    13

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured