After a disasterous last attempt at programming, I tried my hand at something a little easyer! Namely... Encryption! I'm quite pleased with my current handy-work... but there's something that I don't quite understand.
The following program (and it's Decrypting counterpart) work fine on .txt and .bmp files... but for some odd reason it mangles .jpg, .exe, .doc and a bunch of other file formats.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
/* The following variables are (in order of apearance):
Object for File Input || Object for File Output || Variable to hold the Input Filename ||
Variable to hold the Output Filename || Variable for the Current Character || Variable to
put the Encrypted character in || Integer to put the characters ASCII value || Boolean that states
wheter this is the first cycle or not || Integer holding the file size
*/
ifstream Fileop;
ofstream DestFil;
char Filenam[32];
char OutFile[32];
char c;
char e;
string ch;
int asc;
bool firstrun = true;
long begin,end;
cout << "Encryptor\n-----------\nCreated by:Rider Rockon"; // A little bit of interface
cout << "\n\n\nFile to Encrypt:> ";
cin >> Filenam; // Wait for input, make Filenam whatever was typed
cout << "Destination File:> ";
cin >> OutFile; // Wait for input, make OutFile whatever was typed
cout << "\n Opening Input: " << Filenam << " and Output: " << OutFile << "\n";
Fileop.open (Filenam);
DestFil.open(OutFile,ios::trunc);
// Count File size & Return the cursor to the start of the file
begin = Fileop.tellg();
Fileop.seekg (0, ios::end);
end = Fileop.tellg();
Fileop.seekg (0, ios::beg);
cout << "Encrypting " << (end-begin) << " bytes of Data...\n";
while (Fileop.good()) // As long as there's no Error or End of File
{
ch = "";
if ( firstrun == false )
{
// This is where the Encrypting takes place, when done, put it in ch
asc = asc + 1; ch = asc;
// Write ch to the file
DestFil << ch;
}
else
{ firstrun = false; }
c = Fileop.get(); // Get the next character in the Input file, put it in c
asc = c; // asc now contains the ASCII value of c
};
cout << "\nDone...\n";
Fileop.close();
DestFil.close();
return 0;
}
I'm still sort of new to all of this and I'm not really sure where the problem is...
I'd really appreciate any help regarding the subject