Here is a program that encodes and decodes a text file. What I need to do
is write a C++ program that requests 3 different file names. One filename
is for the source file to be encoded, another is the name of the 'encoded'
output file, and the final filename is an offset file. On a character by
character basis, the program needs to look at the first character of the
source file and offset it by the first character in the offset file. The
second character in the source is offset by the second character in the
secret offset file. I need to deal with the possibility that your source
is longer than the offset file in which case it just needs start over or
repeat the secret offset file from the beginning.
The one thing I am having trouble with is offsetting the source file with
the offset file. Here is my code. I need to implement the offset file. Any
ideas?
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
#define cypher (char) ASCII + 1
#define decypher (char) ASCII - 1
int main()
{
char ASCII;
cout << "1) Encode\n2) Decode\n<q to quit>: ";
int coding;
cin >> coding;
if(coding == 1)
{
ifstream source;
ofstream clone;
source.open("lazy_dog.txt", ios::in);
clone.open("cypher.txt", ios::out | ios::trunc);
while(!source.eof())
{
char NewASCII;
ASCII = source.get();
if (source.fail())
return 0;
NewASCII = cypher;
clone.put(NewASCII);
}
source.close();
clone.close();
return 0;
}
else if(coding == 2)
{
ifstream origin;
ofstream copy;
origin.open("cypher.txt", ios::in);
copy.open("decypher.txt", ios::out | ios::trunc);
while(!origin.eof())
{
char NewASCII;
ASCII = origin.get();
if (origin.fail())
return 0;
NewASCII = decypher;
copy.put(NewASCII);
}
origin.close();
copy.close();
return 0;
}
else
return 0;
}