问题=试图弄清楚为什么要写入文件
myfile.open ("Crypt103Data.txt", ios::out | ios::app);
myfile <<tab2[code1];
myfile.close();
将最后一个字符写入Crypt103Data.txt两次,同时在while循环中运行相同迭代的cout显示正确而没有附加到文件的额外字符写入?
如果您输入类似Hello_World的内容,您可能会看到类似于r $ cc7fq7UcX的内容,具体取决于输入的种子值,这会为输出提供依赖于种子密钥的唯一输出。虽然在Hello_World下面的屏幕上正确观察到r $ cc7fq7UcX,但写入文件的是r $ cc7fq7UcXX,其中最后一个字符被写入两次,它应该只是r $ cc7fq7UcX。不知道为什么会发生这种情况发生在显示和文件写入附加的cout打印发生在while循环内的相同迭代。在写完每一行之后,不必在文件中放置一个乐队辅助来修剪最后一个字符,而是必须修复这个奇怪的错误,这里有人可能会指出。也许我正在使用草率编程导致这种情况发生。我正在使用Bloodshed Dev c ++ 4.9.9.2 IDE btw
解密时,由于这个额外字符写入输出重复的最后一个字符,它被解密为Hello_Worldd,这就是问题。
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
// 12/30/2015 - Added string random shuffle
// 12/31/2015 - Added custom seed for random shuffle so seed acts a crypt key
// 1/4/2016 - Added ability to enter string and have while loop process v1.00
// 1/5/2016 - Added characters , and ; to supported characters which were missing from v1.01
// 1/8/2015 - Source code trash cleanup & offset band aid removed for IF statement logic +1 array offset v1.02
// 1/8/2015 - Added write to file for coded data storage eliminating need to copy/paste output to a text file v1.03
using namespace std;
int main(int argc, char *argv[])
{
int seed1=0;
int code1=0;
int run=1;
int again=1;
int test=1;
char ch1;
char ch2;
while(again==1){
cout<<" Crypt Version 1.03\n\n";
cout<<"XXXXXXXXXXXXXXXXXXXXX\n";
cout<<" Enter Integer Seed: \n"; // Asks user to input integer seed
cout<<"XXXXXXXXXXXXXXXXXXXXX\n\n";
cin >> seed1; // Input user seed
cout<<"\n\n"<<"Crypt String Key =\n\n";
ofstream myfile;
myfile.open ("Crypt103Key.txt", ios::out | ios::app);
myfile <<seed1<<"\n";
myfile.close();
//Initialize Valid Characters for String Shuffle Output
//Note Bug corrected with blank space for \ by use of escape character proceeding
string str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\\/~.,;";
srand(seed1); // Allows user custom seeded starting algorithm position for random
random_shuffle(str.begin(), str.end()); // Shuffle the string
cout << str << "\n\n\n"; // Output the shuffle sequence
//Pass String Output into an array to pair up pointer value with associated character
string tmp = str; //Pass str output to string tmp
char tab2[128]; // Memory Allocation for array population
strncpy(tab2, tmp.c_str(), sizeof(tab2)); //string copy tmp into tab2 array
tab2[sizeof(tab2) - 1] = 0;
cout<<"Enter Info to Crypt in correct case\n";
cout<<"To Exit Inner Program enter ( ` ) \n\n\n";
while(run==1){
cin>>ch1;
if (ch1=='A'){
code1=0;
}
else if (ch1=='B'){
code1=1;
}
else if (ch1=='C'){
code1=2;
}
else if (ch1=='D'){
code1=3;
}
else if (ch1=='E'){
code1=4;
}
else if (ch1=='F'){
code1=5;
}
else if (ch1=='G'){
code1=6;
}
else if (ch1=='H'){
code1=7;
}
else if (ch1=='I'){
code1=8;
}
else if (ch1=='J'){
code1=9;
}
else if (ch1=='K'){
code1=10;
}
else if (ch1=='L'){
code1=11;
}
else if (ch1=='M'){
code1=12;
}
else if (ch1=='N'){
code1=13;
}
else if (ch1=='O'){
code1=14;
}
else if (ch1=='P'){
code1=15;
}
else if (ch1=='Q'){
code1=16;
}
else if (ch1=='R'){
code1=17;
}
else if (ch1=='S'){
code1=18;
}
else if (ch1=='T'){
code1=19;
}
else if (ch1=='U'){
code1=20;
}
else if (ch1=='V'){
code1=21;
}
else if (ch1=='W'){
code1=22;
}
else if (ch1=='X'){
code1=23;
}
else if (ch1=='Y'){
code1=24;
}
else if (ch1=='Z'){
code1=25;
}
else if (ch1=='a'){
code1=26;
}
else if (ch1=='b'){
code1=27;
}
else if (ch1=='c'){
code1=28;
}
else if (ch1=='d'){
code1=29;
}
else if (ch1=='e'){
code1=30;
}
else if (ch1=='f'){
code1=31;
}
else if (ch1=='g'){
code1=32;
}
else if (ch1=='h'){
code1=33;
}
else if (ch1=='i'){
code1=34;
}
else if (ch1=='j'){
code1=35;
}
else if (ch1=='k'){
code1=36;
}
else if (ch1=='l'){
code1=37;
}
else if (ch1=='m'){
code1=38;
}
else if (ch1=='n'){
code1=39;
}
else if (ch1=='o'){
code1=40;
}
else if (ch1=='p'){
code1=41;
}
else if (ch1=='q'){
code1=42;
}
else if (ch1=='r'){
code1=43;
}
else if (ch1=='s'){
code1=44;
}
else if (ch1=='t'){
code1=45;
}
else if (ch1=='u'){
code1=46;
}
else if (ch1=='v'){
code1=47;
}
else if (ch1=='w'){
code1=48;
}
else if (ch1=='x'){
code1=49;
}
else if (ch1=='y'){
code1=50;
}
else if (ch1=='z'){
code1=51;
}
else if (ch1=='1'){
code1=52;
}
else if (ch1=='2'){
code1=53;
}
else if (ch1=='3'){
code1=54;
}
else if (ch1=='4'){
code1=55;
}
else if (ch1=='5'){
code1=56;
}
else if (ch1=='6'){
code1=57;
}
else if (ch1=='7'){
code1=58;
}
else if (ch1=='8'){
code1=59;
}
else if (ch1=='9'){
code1=60;
}
else if (ch1=='0'){
code1=61;
}
else if (ch1=='!'){
code1=62;
}
else if (ch1=='@'){
code1=63;
}
else if (ch1=='#'){
code1=64;
}
else if (ch1=='$'){
code1=65;
}
else if (ch1=='%'){
code1=66;
}
else if (ch1=='^'){
code1=67;
}
else if (ch1=='&'){
code1=68;
}
else if (ch1=='*'){
code1=69;
}
else if (ch1=='('){
code1=70;
}
else if (ch1==')'){
code1=71;
}
else if (ch1=='_'){
code1=72;
}
else if (ch1=='-'){
code1=73;
}
else if (ch1=='+'){
code1=74;
}
else if (ch1=='='){
code1=75;
}
else if (ch1=='?'){
code1=76;
}
else if (ch1=='<'){
code1=77;
}
else if (ch1=='>'){
code1=78;
}
else if (ch1==':'){
code1=79;
}
else if (ch1=='\\'){ // Escape Character \ needed to allow \ check
code1=80;
}
else if (ch1=='/'){
code1=81;
}
else if (ch1=='~'){
code1=82;
}
else if (ch1=='.'){
code1=83;
}
else if (ch1==','){
code1=84;
}
else if (ch1==';'){
code1=85;
}
else if (ch1=='\`'){ //Escape Character \ before ` to exit
run=0; //Run = False at 0 and leaves while loop
}
else {
cout<<"Invalid Input = No Match\n\n";
}
cout<<tab2[code1];
myfile.open ("Crypt103Data.txt", ios::out | ios::app);
myfile <<tab2[code1];
myfile.close();
}// end inner while loop
//Add line return in file to seperate the data per line
myfile.open ("Crypt103Data.txt", ios::out | ios::app);
myfile <<" "<<"\n";
myfile.close();
test=1;
while(test==1){
system("CLS");
cout<<"Enter Y to continue or N to end \n\n\n";
cin>>ch2;
if (ch2=='N'||ch2=='n'){
again=0;
test=0;
}
else if (ch2=='Y'||ch2=='y'){
again=1;
test=0;
run=1;
system("CLS");
}
else {
cout<<"Invalid Input, please choose Y or N \n\n";
test=1;
}
}
} // end outter while loop
system("CLS");
system("PAUSE");
return EXIT_SUCCESS;
}