-
Allright, after a bit of messing around with the code (and your second link, vart) I've come to create the following:
Code:
while (Fileop.good()) // As long as there's no Error or End of File
{
if ( firstrun == false ) { DestFil.write(reinterpret_cast < char * > (&och), sizeof(och)); };
Fileop.read(reinterpret_cast < char * > (&ich), sizeof(ich));
asc = (int)ich;
// Encrypt ASC here preparing it for output
asc = asc + 1;
och = (unsigned char)asc;
if ( firstrun == true ) { firstrun = false; };
};
(Asc is an INT, ich and och both Unsigned CHAR's, firstrun is a boolean)
But something goes wrong, because the file being encrypted is reduced in size greatly (My 413KB file became 3.50KB) and I can't quite figure out where I'm messing up.
My guess would be that something gets 'lost in translation' while being converted back and forth from char to int to char again, but like I said, I don't know exactly what's going awry here.
Any help on the matter is greatly appreciated :)
-
where ich and och are defined?
-
They're defined above that, I didn't think it'd be that important, but regardless, this is where they are defined:
Code:
ifstream Fileop;
ofstream DestFil;
char Filenam[32];
char OutFile[32];
unsigned char ich;
unsigned char och;
int asc;
bool firstrun = true;
long begin,end;
-
1. you can remove reinterpreter cast
2. you can remove ; after }
3. you should check the number of bytes written by the write function, if it is 0 - you should check what charaster cause this
4. have you tried to write the file without changing value? it is working as should?
-
Regarding 1, I'm not quite sure what the right way would be to go about that, just removing it b0rks the code :)
on 3, I made the program output the size of every character read, and already it's coming up short on bytes...
4. I've done the following:
Just reading and writing without converting the values to another type of variable (basically the example provided by BobS0327) and it works fine, the entire file is duplicated.
Reading and writing without changing the value, but WITH converting it from the read procedure through all the variables, and back to the write procedure. This didn't work.
-
*Bump*
I'd really like some help on this issue... my current Encryption program looks like this:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main (char*)
{
ifstream Fileop;
ofstream DestFil;
char Filenam[32];
char OutFile[32];
unsigned char ich;
unsigned char och;
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
{
if ( firstrun == false ) { DestFil.write(reinterpret_cast < char * > (&och), sizeof(och)); };
Fileop.read(reinterpret_cast < char * > (&ich), sizeof(ich));
asc = (int)ich;
// Encrypt ASC here preparing it for output
asc = asc + 1;
och = (unsigned char)asc;
if ( firstrun == true ) { firstrun = false; };
};
cout << "\nDone...\n";
Fileop.close();
DestFil.close();
return 0;
}
as stated above, the file being encrypted is reduced in size greatly (My 413KB file became 3.50KB) and I can't quite figure out where I'm messing up.
Could anyone please help me out? I'm just a beginning programmer and I haven't got too much experience so far... heck... I'm suprised I even got this far!
Any help on the matter would be greatly appreciated :)
-
hmmm...
Have you tried the suggestion of the BobS0327?
Code:
ifstream MyInFile (argv[1], ios::in | ios::binary);
ofstream MyOutFile (argv[2], ios::out | ios::binary);
-
-
-
The first step in writing any file transformation program is to simply write something which copies the input to the output without changing it at all. Until you can manage that, the rest of the problem doesn't matter.
Code:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream Fileop ( "a.exe", ios::in | ios::binary );
ofstream DestFil( "copy.bin", ios::out | ios::binary );
char ch;
while ( Fileop.get(ch) ) {
DestFil.put(ch);
}
Fileop.close();
DestFil.close();
return 0;
}
$ # copying itself
$ ls -l a.exe ; ./a.exe ; ls -l copy.bin ; cmp a.exe copy.bin
-rwxr-xr-x 1 who None 477334 Dec 22 17:34 a.exe
-rw-r--r-- 1 who None 477334 Dec 22 17:35 copy.bin
-
Yes well, I already accomplished that earlyer. The only problem is that my method of encryption (by recalculating the ASCII value of the read character) requires the character to be moved through other variables... somewhere in that particular process, something goes wrong.
This routine:
Code:
while (Fileop.good()) // As long as there's no Error or End of File
{
if ( firstrun == false ) { DestFil.write(reinterpret_cast < char * > (&och), sizeof(och)); };
Fileop.read(reinterpret_cast < char * > (&ich), sizeof(ich));
// asc = (int)ich;
Fileop.read(reinterpret_cast < char * > (&pos), sizeof(pos));
// Encrypt ASC here preparing it for output
// asc = asc + 1;
// och = (unsigned char)asc;
if ( firstrun == true ) { firstrun = false; };
};
This was the original routine, kudos to BobS0327, and it worked fine... But I'll try your version Salem... it looks a lot more compact at least (and, for a coding newb like me, a lot more clear on what it does :D)
-
Well it's really rather simple,
From this
Code:
while ( Fileop.get(ch) ) {
DestFil.put(ch);
}
To this
Code:
while ( Fileop.get(ch) ) {
ch = myEncrypt(ch);
DestFil.put(ch);
}
Before messing with files, you can test the function with very basic tests like
Code:
int main ( ) {
cout << myEncrypt('A');
// or
for ( int i = 'A' ; i <= 'Z' ; i++ ) cout << myEncrypt( i );
}
When both ideas are working well separately, then you can try bringing them together.
It doesn't hurt to have a couple of 'test' projects on the side containing just a main(), where you can work on a specific idea in isolation. When you've got it working, then simply copy/paste what you want to the real project.