Thread: Writing encrypted value to file.

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    22

    Writing encrypted value to file.

    Well, I'm having a little problem writing an encrypted value to a file.

    To be a little more specific, I used an XOR like method to encrypt a string, and tried writing the value to a file. The problem is, it's only writing the first character in the string, or nothing at all.

    Here's a code snippet:
    Code:
    string4 = "Text: "+string1+"\nText: "+string2+"\nText: "+string3;
    				for ( unsigned int i = 0; i < string4.length(); i++ ) {
    					string4[i] = string4[i]^key[i];
    					string4 = string4[i];
    				}
    				ofstream file;
    				path_source = "C:\\Users\\%USER%\\Documents\\dir\\";
    				path = path_source+string1+path_ext;
    				file.open (path.c_str());
    				file << string4;
    Does anyone see any problems, or know why this may be occuring?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You need to open the file in binary mode.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    22
    Quote Originally Posted by hk_mp5kpdw View Post
    You need to open the file in binary mode.
    Thanks, I knew it had something to do with how the file was opened.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    22
    Okay, well, I tried adding ios::binary, and nothing is still written to the file.

    Do I need to open and write to the file using a different method, now that I'm using binary mode?

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    Quote Originally Posted by mkthnx001 View Post
    Here's a code snippet:
    Code:
    string4 = "Text: "+string1+"\nText: "+string2+"\nText: "+string3;
    				for ( unsigned int i = 0; i < string4.length(); i++ ) {
    					string4[i] = string4[i]^key[i];
    					string4 = string4[i];
    				}
    				ofstream file;
    				path_source = "C:\\Users\\%USER%\\Documents\\dir\\";
    				path = path_source+string1+path_ext;
    				file.open (path.c_str());
    				file << string4;
    Does anyone see any problems, or know why this may be occuring?
    That red part there, it doesn't seem right, you're assigning a char to a string object? are you sure you need it there? As for how you're supposed to write binary data to the file, try looking into this:

    write - C++ Reference

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    22
    Quote Originally Posted by JacobN View Post
    That red part there, it doesn't seem right, you're assigning a char to a string object? are you sure you need it there? As for how you're supposed to write binary data to the file, try looking into this:

    write - C++ Reference
    Well, I replaced file.open (path.c_str()); with file.open (path.c_str(),ios::binary);, and it still doesn't seem to be working.

    Is there something else I must do?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The standard library won't expand %USER%, that's a shell thing.

    char *user = getenv("USER");
    check it for NULL, then substitute it yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    22
    Quote Originally Posted by Salem View Post
    The standard library won't expand %USER%, that's a shell thing.

    char *user = getenv("USER");
    check it for NULL, then substitute it yourself.
    No, that's not the problem, I just put the %USER% in the code snippet, to replace the actual value that was in that place.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Fantastic, complete waste my of time on a non-existent problem
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    22
    Anywho, if someone would be kind enough to explain the binary opening mode to me a bit more in detail, I'd really appreciate it.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    Quote Originally Posted by mkthnx001 View Post
    Anywho, if someone would be kind enough to explain the binary opening mode to me a bit more in detail, I'd really appreciate it.
    No offense, but did you check out the link I gave you? There's a full example showing how to use ofstream::write to write binary data to a file... Or at least that's what I thought.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    22
    Quote Originally Posted by JacobN View Post
    No offense, but did you check out the link I gave you? There's a full example showing how to use ofstream::write to write binary data to a file... Or at least that's what I thought.
    Yes, but I'm afraid I don't quite understand it. Isn't the example they show for copying one file's contents to another?

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    It is, but notice how they do it? They read the data from one file into a char array, just like you have in your string object( the c_str() method returns one ), and then they write this char array to the other file using ofstream::write. To get the number of chars you're going to write to the file, you could use the string::length() method to get this number.

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    22
    Is there any other method of doing this?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM