出于教育目的,我正在编写一个小型加密程序(不是真正的加密)。我已对该程序进行了重组,因此我的所有代码都是主要用于简化自己的事情。
我的unEncrypted文件失败。我不知道为什么。这就是我目前所知道的。
以下是我的全部代码。
// This program reads the contents of a file, encrypts it, and write the contents into a separate file.
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <cmath>
using namespace std;
// Global Constants
const int POSITIVE_INT_LIMIT = 10;
const int NEGATIVE_INT_LIMIT = -10;
const int SLOPE_NEGATIVE = -1;
const int SLOPE_POSITIVE = 1;
const int STEP = 1;
int main() {
int amplitude = 0, slope = SLOPE_NEGATIVE; //Set initial amplitude and slope.
int streamSize = 1;
char ch, cy;
char *chPtr = &cy; // Initialize and assign char pointer.
//open an unencrypted file to read from
fstream unEncryptedFile;
unEncryptedFile.open("testA", ios::in | ios::binary);
//open a file to write encrypted info
fstream cryptFile;
cryptFile.open("testB", ios::out | ios::binary);
//Clear flags previous set, just in case
unEncryptedFile.clear();
cryptFile.clear();
//Rewind the files, just in case
unEncryptedFile.seekg(0L, ios::beg);
cryptFile.seekp(0L, ios::beg);
if (unEncryptedFile.fail()) {
cout << "Error opening read file." << endl;
exit(1);
}
if (cryptFile.fail()) {
cout << "Error opening write file." << endl;
exit(1);
}
/* Encryption pattern inside while-loop.
limit> 10 *
9 * *
8 * *
7 * *
6 * *
5 * *
4 * *
3 * *
2 * *
1 * *
start> 0*2345678901234567890*2345678901234567890* -- < one hertz (cycle)
-1 * *
-2 * * (Number line: each integer represents a single while-loop cycle.)
-3 * *
-4 * *
-5 * *
-6 * *
-7 * *
-8 * *
-9 * *
limit> -10 *
*/
/*************************************************************
The pattern above depictes a single character
being read, and then the value of amplitude is added to it.
*************************************************************/
while (!unEncryptedFile.fail()) {
ch = unEncryptedFile.get(); // Get the next character in the file.
cout << ch << endl; // test for proper read
if (amplitude > NEGATIVE_INT_LIMIT && slope == SLOPE_NEGATIVE) {
amplitude -= STEP;
cy = ch + amplitude;
cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.
} else if (amplitude <= NEGATIVE_INT_LIMIT){
slope = SLOPE_POSITIVE;
amplitude = NEGATIVE_INT_LIMIT;
cy = ch + amplitude;
cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.
} else if (amplitude < POSITIVE_INT_LIMIT && SLOPE_POSITIVE){
amplitude += STEP;
cy = ch + amplitude;
cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.
} else if (amplitude >= POSITIVE_INT_LIMIT){
slope = SLOPE_NEGATIVE;
amplitude = POSITIVE_INT_LIMIT;
cy = ch + amplitude;
cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.
}
}
//Files are closed.
unEncryptedFile.close();
cryptFile.close();
return 0;
}
答案 0 :(得分:0)
您需要在打开文件时提供带文件名的文件扩展名
int main() {
int amplitude = 0, slope = SLOPE_NEGATIVE; //Set initial amplitude and slope.
int streamSize = 1;
char ch, cy;
char *chPtr = &cy; // Initialize and assign char pointer.
//open an unencrypted file to read from
fstream unEncryptedFile;
unEncryptedFile.open("testA.txt", ios::in | ios::binary);
我尝试使用此代码并且它正在运行。