Minime80
11-11-2000, 11:35 PM
Okay, I'm trying to make a simple program that encrypts a text file by adding 10 to the ASCII value of each character. Here's what I've got:
#include <fstream.h>
void setupfiles(ifstream &, ofstream &);
void encrypt(ifstream &, ofstream &);
int main()
{
ifstream Input;
ofstream Output;
setupfiles(Input, Output);
encrypt(Input, Output);
Input.close();
Output.close();
}
void setupfiles(ifstream &In, ofstream &Out)
{
char InFile[256], OutFile[256];
cout << "\nWhat file would you\nlike to encrypt (full filename): ";
cin.getline(InFile, 256);
In.open(InFile, ios::nocreate);
while (In.fail())
{
cout << "File does not exist or could not be found\nPlease try again: ";
cin.getline(InFile, 256);
In.open(InFile, ios::nocreate);
}
cout << "\nWhere would you like to\nsave the encrypted file (full filename): ";
cin.getline(OutFile, 256);
Out.open(OutFile, ios::noreplace);
while (Out.fail())
{
cout << "That file already exists.\nPlease try again: ";
cin.getline(OutFile, 256);
Out.open(OutFile, ios::noreplace);
}
}
void encrypt(ifstream &In, ofstream &Out)
{
char Ch;
while (!In.eof())
{
In.get(Ch);
Out.put(Ch+10);
}
cout << "\n\nFile has been encrypted.\n";
}
The problem is that whenever I run it it makes the output file, but when it's done the output file is always empty. Can anyone tell me what I'm doing wrong here?
#include <fstream.h>
void setupfiles(ifstream &, ofstream &);
void encrypt(ifstream &, ofstream &);
int main()
{
ifstream Input;
ofstream Output;
setupfiles(Input, Output);
encrypt(Input, Output);
Input.close();
Output.close();
}
void setupfiles(ifstream &In, ofstream &Out)
{
char InFile[256], OutFile[256];
cout << "\nWhat file would you\nlike to encrypt (full filename): ";
cin.getline(InFile, 256);
In.open(InFile, ios::nocreate);
while (In.fail())
{
cout << "File does not exist or could not be found\nPlease try again: ";
cin.getline(InFile, 256);
In.open(InFile, ios::nocreate);
}
cout << "\nWhere would you like to\nsave the encrypted file (full filename): ";
cin.getline(OutFile, 256);
Out.open(OutFile, ios::noreplace);
while (Out.fail())
{
cout << "That file already exists.\nPlease try again: ";
cin.getline(OutFile, 256);
Out.open(OutFile, ios::noreplace);
}
}
void encrypt(ifstream &In, ofstream &Out)
{
char Ch;
while (!In.eof())
{
In.get(Ch);
Out.put(Ch+10);
}
cout << "\n\nFile has been encrypted.\n";
}
The problem is that whenever I run it it makes the output file, but when it's done the output file is always empty. Can anyone tell me what I'm doing wrong here?