fstream, fail()
-
I am trying to create a simple to program to encrypt a file but I am having a lot of trouble with accessing the file. Firstly, whenever I try to access the file fstream's fail() always returns true. If I take the check to fail() from my code it works fine encrypting the file but when I go to decrypt it there are artifacts in the file (2 extra bytes in my test file). here is the code in question:
#define macro_encrypt (int) Byte + 25
#define macro_decrypt (int) Byte - 25void fn_Decrypt()
{
char oldFilename[200];
char newFilename[200];cout << "Enter Filename: "; cin >> oldFilename; cout << endl; cout << "Enter new Filename: "; cin >> newFilename; cout << endl; ifstream infile; ofstream outfile; char Byte; infile.open(oldFilename, ios::in | ios::binary); outfile.open(newFilename, ios::out | ios::binary); while (!infile.eof()) { char NewByte; Byte = infile.get(); NewByte = macro\_decrypt; outfile.put(NewByte); } infile.close(); outfile.close(); fn\_start();
}
I input a text file:
hello encryption
I get this (notepad output):
~……ˆ9~‡|‹’‰‚ˆ‡
And when I go to decrypt that file I get this:
hello encryptionÿæ
What makes this all the more frustrating is that when I compile the code this is based on it works fine. I can't see why it would work and mine won't, they are functionally identical (from what I can see). Does anyone know why this is happening and/or how to fix it?
-
I am trying to create a simple to program to encrypt a file but I am having a lot of trouble with accessing the file. Firstly, whenever I try to access the file fstream's fail() always returns true. If I take the check to fail() from my code it works fine encrypting the file but when I go to decrypt it there are artifacts in the file (2 extra bytes in my test file). here is the code in question:
#define macro_encrypt (int) Byte + 25
#define macro_decrypt (int) Byte - 25void fn_Decrypt()
{
char oldFilename[200];
char newFilename[200];cout << "Enter Filename: "; cin >> oldFilename; cout << endl; cout << "Enter new Filename: "; cin >> newFilename; cout << endl; ifstream infile; ofstream outfile; char Byte; infile.open(oldFilename, ios::in | ios::binary); outfile.open(newFilename, ios::out | ios::binary); while (!infile.eof()) { char NewByte; Byte = infile.get(); NewByte = macro\_decrypt; outfile.put(NewByte); } infile.close(); outfile.close(); fn\_start();
}
I input a text file:
hello encryption
I get this (notepad output):
~……ˆ9~‡|‹’‰‚ˆ‡
And when I go to decrypt that file I get this:
hello encryptionÿæ
What makes this all the more frustrating is that when I compile the code this is based on it works fine. I can't see why it would work and mine won't, they are functionally identical (from what I can see). Does anyone know why this is happening and/or how to fix it?
The artifacts are due to the way you're controlling the loop. Check what std::istream::get actually returns. Note that your cyphertext already has an extra character in it so your encrypt is part of the problem. Just out of interest, as you're programming in C++ and not C: - get rid of all the explicit opens/closes, you don't need them, destructors are wonderful things - instead of letting a user crash your code by entering a filename of longer than 200 characters use std::strings and std::getline - don't use macros, a pair of functions would have worked as well and enabled you to use the same code for encryption and decryption (the same goes for C BTW) I'd also consider splitting the encrypt/decrypt bits of your code from all the guff asking for filenames, then you'd have something you can unit test without all the nuts ache of entering files all the time. Cheers, Ash