orufet
09-05-2004, 05:18 AM
Right. So, I'm writing a program that reads text from a file, encryptions it (just by shifting the characters over one) and writes to a new file. The problem is, however, that once the program is finished running, my terminal seems to be encryped as well...
I'm not an experienced programmer, but I know that my code worked at some point. Perhaps you guys could figure this one out?
Here's the main function:
int opendecfile() {
char path[256];
char newpath[256];
int confirm=0;
int buf=0;
const int BUF_MAX=512;
// open a normal file to be encrypted
while(confirm==0) {
cout << "\nPlease type the full path of the file: ";
cin >> path;
cout << "\nThe file you chose to encrypt is " << path;
cout << "\nIs this correct? [y=1/n=0] ";
cin >> confirm;
if(confirm==1) {
// they got the path right
break;
}
}
cout << "\nEncrypting file " << path << "\n";
// open the file for reading and encrypt it as we go
ifstream decfile(path);
char buffer[BUF_MAX];
if(!decfile.is_open()) {
cout << "\nError opening file...\n";
exit(1);
}
cout << "Please type the name of the new file: ";
cin >> newpath;
cout << "New file contents:";
cout << "\n\n---\n\n";
ofstream encfile(newpath); // the file to which we're writing
while(!decfile.eof()) {
decfile.getline(buffer,100);
for(buf=0;buf<=strlen(buffer);buf++) {
// strcpy(buffer,buffer);
// modify each character
buffer[buf]=buffer[buf]-1;
// cout << buffer[buf];
}
cout << buffer << endl;
// write encrypted file
if(encfile.is_open()) {
encfile << buffer;
}
// cout << endl;
}
encfile.close();
decfile.close();
cout << "\n\n---\n\n";
cout << "Done encrypting!\n";
// delete original file
return 0;
}
And a paste of the output:
New file contents:
---
gdkkn+sghrhr`sdrsnelxdmbqxoshnmoqnfq`l�?┘ÿ ÿÿÿÿÿÿÿÿóþ¾V9␋ÿá ┐ÿº␌_ÿ┬j��������������k��7k�߱j�/�
i������������U8h���j��b��v␋þþþþþþþþþþþþ¢ ┘þþ6┘þÞ °␋þ.³ÿ?ÿÿÿK·
ýýýýýýýýýýýý¡␋ýý5␋ýݯý-²þ>þþþJ¶ÿ?§¸⎺┌␍⎺^┘ý=Ýýýýýýýýýýñü¼T7±ýß␋ý¸▒«ý┤
│├␊⎼└
---
D⎺┼␊ ␊┼␌⎼≤⎻├␋┼±!
[┘▒␌⎺␉@S0106000802°46␍␊8 ⎽⎼␌]$ │├␊⎼└
I hope the error was stupid and obvious...thanks
I'm not an experienced programmer, but I know that my code worked at some point. Perhaps you guys could figure this one out?
Here's the main function:
int opendecfile() {
char path[256];
char newpath[256];
int confirm=0;
int buf=0;
const int BUF_MAX=512;
// open a normal file to be encrypted
while(confirm==0) {
cout << "\nPlease type the full path of the file: ";
cin >> path;
cout << "\nThe file you chose to encrypt is " << path;
cout << "\nIs this correct? [y=1/n=0] ";
cin >> confirm;
if(confirm==1) {
// they got the path right
break;
}
}
cout << "\nEncrypting file " << path << "\n";
// open the file for reading and encrypt it as we go
ifstream decfile(path);
char buffer[BUF_MAX];
if(!decfile.is_open()) {
cout << "\nError opening file...\n";
exit(1);
}
cout << "Please type the name of the new file: ";
cin >> newpath;
cout << "New file contents:";
cout << "\n\n---\n\n";
ofstream encfile(newpath); // the file to which we're writing
while(!decfile.eof()) {
decfile.getline(buffer,100);
for(buf=0;buf<=strlen(buffer);buf++) {
// strcpy(buffer,buffer);
// modify each character
buffer[buf]=buffer[buf]-1;
// cout << buffer[buf];
}
cout << buffer << endl;
// write encrypted file
if(encfile.is_open()) {
encfile << buffer;
}
// cout << endl;
}
encfile.close();
decfile.close();
cout << "\n\n---\n\n";
cout << "Done encrypting!\n";
// delete original file
return 0;
}
And a paste of the output:
New file contents:
---
gdkkn+sghrhr`sdrsnelxdmbqxoshnmoqnfq`l�?┘ÿ ÿÿÿÿÿÿÿÿóþ¾V9␋ÿá ┐ÿº␌_ÿ┬j��������������k��7k�߱j�/�
i������������U8h���j��b��v␋þþþþþþþþþþþþ¢ ┘þþ6┘þÞ °␋þ.³ÿ?ÿÿÿK·
ýýýýýýýýýýýý¡␋ýý5␋ýݯý-²þ>þþþJ¶ÿ?§¸⎺┌␍⎺^┘ý=Ýýýýýýýýýýñü¼T7±ýß␋ý¸▒«ý┤
│├␊⎼└
---
D⎺┼␊ ␊┼␌⎼≤⎻├␋┼±!
[┘▒␌⎺␉@S0106000802°46␍␊8 ⎽⎼␌]$ │├␊⎼└
I hope the error was stupid and obvious...thanks