Example how to do so. Looking at you xor that needs some work. Its a simple process don't overcomplicate it by trying to use syntax tricks.
Code:#include <iostream> #include <fstream> #include <string> int main() { //This will hold the files contents char *fileContents = NULL; std::ifstream file("file.txt", std::ios::binary); //Could not open the file, break out if(!file){ return 0; }//if //Seek to the end of the file file.seekg(0, std::ios::end); //Grab the get pointers location, this will be how many items the //file contains size_t length = file.tellg(); //Seek back to the beginning of the file file.seekg(0, std::ios::beg); //Allocate the char to the number of characters in the file fileContents = new char[length]; //Read the whole file in file.read(fileContents, length); //Close the file out file.close(); //Loop through all the characters int the file for(size_t i = 0; i < length; i++){ char c = fileContents[i]; //Encryption here }//for //Make sure to clean up delete [] fileContents; return 0; }
c would be the character I would xor encrypt if I was doing it
So I'm assuming I'm supplying the encrypting - just asking. BTW, it cannot find the file "test.txt". It's sitting write next to it in the folder-I even supplied the file path. It still is saying that it can't find it. What's wrong?
That was just an example file. I know sometimes if you name a file it can actually end up being this testing.txt.txt. Therefore the program can't find it.
You can use the same structure as you had before for opening the file. That was just example code to show you how to read in a whole file and process each character for your encryption.
Ok I'm probably starting to sound real stupid now, but am I supposed to encrypt "char c" or "fileContents", and whichever I use, do I use the encryption method from my previous posts? Sorry if I'm getting annoying...
http://www.cprogramming.com/tutorial/xor.html
c would be the character to encrypt.
You will need some place to hold your encrypted characters. Could be a char * or a std::string. That I leave up to you.
You could also just xor the contents of fileContents as well. Same result different methodCode:char c = fileContents[i]; c = c ^ key buffer[i] = c;
Code:fileContents[i] = fileContents[i] ^ key
Last edited by prog-bman; 08-08-2007 at 08:15 PM.
Does anyone know how to convert a character array to a string? I have to following but it doesn't work:
Out being the string, value being the character array, and x being the size of the character array.Code:for (int i = 0; i < x; i++) { out = value[i]; }
A null terminated character array is a C style string.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I meant a std::string, not C style string.A null terminated character array is a C style string.
std::string has a constructor that takes a null terminated C style string as its argument. If your char array is not null terminated, use the constructor that takes a range.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)