Reputation: 849
Not as in "can't find the answer on stackoverflow", but as in "can't see what I'm doing wrong", big difference!
Anywho, the code is attached below. What it does is fairly basic, it takes in a user created text file, and spits out one that has been encrypted. In this case, the user tells it how many junk characters to put between each real character. (IE: if I wanted to encrypt the word "Hello" with 1 junk character, it would look like "9H(eal~l.o")
My problem is that for some reason, it isn't reading the input file correctly. I'm using the same setup to read in the file as I had done previously on decrypting, yet this time it's reading garbage characters, and when I tell it to output to file, it prints it on the screen instead, and it seems like nothing is being put in the output file (though it is being created, so that means I've done something correctly, point for me!
code:
string start;
char choice;
char letter;
int x;
int y;
int z;
char c;
string filename;
while(start == "enc")
{
x = 1;
y = 1;
cout << "How many garbage characters would you like between each correct character?: " ;
cin >> z;
cout << endl << "Please insert the name of the document you wish to encrypt, make sure you enter the name, and the file type (ie: filename.txt): " ;
cin >> filename;
ifstream infile(filename.c_str());
ofstream outfile("encrypted.txt", ios::out);
while(!infile.eof())
{
infile.get(letter);
while ((x - y) != z)
{
outfile << putchar(33 + rand() % 94);
x++;
}
while((x - y) == z)
{
outfile << letter;
y = 1;
x = 1;
}
}
outfile.close();
cout << endl << "Encryption complete...please return to directory of program, a new file named encrypted.txt will be there." << endl;
infile.close();
cout << "Do you wish to try again? Please press y then enter if yes (case sensitive).";
cin >> choice;
What I pasted above the start of the while loop are the declaration variables, this is part of a much larger code that not only will encrypt, but decrypt as well, I left the decryption part out as it works perfectly, it's this part I'm having an issue with.
Thanks in advance for the assist!
EDIT:: I'm using visual C++ express 2008, and it shoots back that there are no errors at all, nor any warnings.
IMPORTANT EDIT It turns out it is outputting to the file! However, it is outputting numbers instead of ascii characters, and it is also outputting the garbage character for the letter it should be. When it goes back to the "infile.get(letter)", it doesn't get a new character. So right now it seems to be the issues are 2 fold: 1) Printing numbers instead of ascii characters. 2) Using garbage instead of the actual character it should be getting.
Question Answered Found out the second part in the "Important Edit" ...it turns out if you name something test.txt...that means it is actually called test.txt.txt when you type it into a C++ program. Just goes to show it's the tiny, minute, simple details that cause any program to go pooey. Thank you to George Shore. Your comment about the input file being in the wrong place is what gave me the idea to try the actual items name.
Thank you to everyone who helped with the answer!
Upvotes: 1
Views: 513
Reputation: 264641
Rather than doing this:
while (!infile.eof())
{
infile.get(letter);
if (infile.good())
{
Do this:
while (infile.get(letter))
{
This is the standard pattern for reading a file.
It gets a character and the resulting infile (that is returned by get) is then checked to see if it is still good by converting it to bool.
The line:
outfile << putchar(33 + rand() % 94);
Should probably be:
outfile << static_cast<char>(33 + rant() % 94);
putchar() prints to the standard output. But the return value (same as the input) goes to the outfile. To stop this just convert the value to char and send to outfile.
Upvotes: 3
Reputation: 4381
Further to the previous answers, I believe it's because the file you wish to encrypt is not being found by the original code. Is it safe to assume that you're running the code from the IDE? If so, then the file that is to be encrypted has to be in the same directory as the source.
Also:
outfile << putchar(33 + rand() % 94);
seems to be the source of your garbage to the screen; the 'putchar' function echoes to the screen whilst returning the integer value of that character. What is then going to happen is that number will be output to the file, as opposed to the character.
Changing that block to something like:
while ((x - y) != z)
{
c = (33 + rand() % 94);
outfile << c;
x++;
}
should enable the code to run as you want it to.
Upvotes: 4
Reputation: 754760
Is the use of 'y
' necessary? It seems confusing and unnecessary to me. If I were implementing this, then I'd expect to use just 'x
' and 'z
'.
I'm also not sure about the 'while (!infile.eof())
' condition; Pascal determines EOF ahead of time, but C++ can only tell you about EOF after attempting to read a character. However, this would only affect the end of the file, not the main body of the loop.
while (!infile.eof())
{
infile.get(letter);
if (infile.good())
{
for (int i = 0; i < z; i++)
outfile << putchar(33 + rand() % 94);
outfile << letter;
}
}
(Uncompiled code!).
Also, do not use this for security - it may help a little, but it certainly won't conceal the information from the determined.
Upvotes: 0