"25 characters of good health" was a noncontextual example.my bad. Allow me to elaborate:
class xor_file is an object meant to control i/o, encryption, decryption and storage of passwords, keys, etc.
xor_file::xor_open() would be the first function called by an xor_file object.
1. Open file
2. See if the file has already been encrypted by the program
2a. If it has been encoded already, read in data regarding the password and key so that it might be decoded later.
2b. If not, simply set the necessary flags to denote an unencrypted file.
3. Return
Now, I had found that after calling the function, I was getting trouble in another function xor_file::xor_encode(). I found that the issue went all the way back to xor_open(), and so I pasted some code from the former function to see what was up. In my code, //DEBUG is beside any line that was added for debugging.
Here is the whole function:
Code:
ios::iostate xor_file::xor_open(string input_path)
{
char* buffer = new char[25];
string sample;
encoded = decoded = xor_open_switch = false; //member switches
reader.open(input_path.c_str(),ios::binary); //open the file
if(reader.fail()) //check for failure
{ //yes, I could have just done if(!reader)
reader.close();
return ios::failbit;
}
//Didn't fail....
xor_open_switch = true;
if(reader.fail()) cout << "something's wrong BEFORE FIRST GETLINE. xor_open()" << endl; //DEBUG
reader.getline(buffer,25,'@'); //sample for c:\windows\desktop\primes.txt
if(reader.fail()) cout << "something's wrong AFTER FIRST GETLINE. xor_open()" << endl; //DEBUG
sample = buffer; //make it a string DEBUG
cout << "open()'s \"sample\": " << sample << endl; //DEBUG
if(sample == "XOR_CODEC_ENCRYPTED_FILE") //This file is already encrtypted
{
encoded = true;
encoded_path = input_path; //set the xor_file path to the valid path
getline(reader,sample,'@'); //sample for password
cout << "Password (encrypted): " << sample << endl; //DEBUG
for(int x = 0; x < sample.size(); x++) sample[x] -= static_cast<char>(1); //TODO: use better algorithm
cout << "Password (decrypted): " << sample << endl; //DEBUG
password = sample;
getline(reader,sample,'@'); //sample for key
cout << "Key: " << sample << endl;
key = sample;
}
else //This file is not encrypted
{
decoded = true;
decoded_path = input_path; //set the xor_file path to the valid path
//reader.seekg(1,ios::beg); //reset reader for encoding later
for(int z = 0; z < 10; z++) cout << reader.get(); //DEBUG
cout << boolalpha << "reader.eof(): " << reader.eof() << endl; //DEBUG
}
Here is the input file (text):
This is a text-only test just to see if the damn
thing works. Of course, as I write this, I haven't
even compiled all of my new functions. It is likely
that I will get tons of error and not even use
this test file for quite a while. Still, I'm not
discouraged. (quite more than 25 chars)
and the output:
Please input path to file.
c:\windows\desktop\input.txt
something's wrong AFTER FIRST GETLINE. xor_open()
open()'s "sample": This is a text-only test
-1-1-1-1-1-1-1-1-1-1reader.eof(): false
Thank you for your help.