【发布时间】:2015-07-08 18:38:59
【问题描述】:
这是我的程序。目的是找到否。文件中的字符数和所有ASCII值的总和 字符数,然后连接编号。字符数+ASCII 值的总和。这将是一个密钥,应该写在文件的末尾。因此,如果有人更改文件文本,重新生成的密钥将与以前不同,程序将通知“文件被某人更改”。 问题是我的密钥没有正确生成。它写 % 而不是 %38757483 这意味着 38 是总字符,757483 是 ASCII 的总和。以下是没有任何语法错误的完整代码:
////By Zeshan from VU
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
////function prototypes///
void menu(std::ifstream &file, string filename); //to show the menu of multiple options
void openFile(string filename);
void findSize(std::ifstream &file, string filename); //to find the sum of all characters in file
void findASCII(char allWords[], int size, std::ifstream &file, string filename); // to find the sum of all character's ASCII values in file
void protectFile(std::ifstream &file, string filename);
void checkFile(std::ifstream &file, string filename);
void key(int size, char ASCII, std::ifstream &file, string filename); // this function will generate the key by combining size figure and ASCII sum followed by % character
void reKey(int size, char ASCII, std::ifstream &file, string filename, string oldKey); //regenerate key for matching with previous one
void reFindASCII(char allWordsBeforeKey, int size, std::ifstream &file, string filename, string oldKey);
void putKeyinFile(string fName, string key, std::ifstream &file); // this function will write the key at file's end line
void reKey(int size, char ASCII, std::ifstream &file, string filename, string oldKey)
{
string key;
char cSize = size;
key=cSize+ASCII;
key='%'+key;
if(key == oldKey)
cout<<"The contents of file are not changed after protection. \n";
else
cout<<"Alert! The contents of file are changed after protection. \n";
system("pause");
}
void checkFile(std::ifstream &file, string filename){
int i=0, flag=0, keyPositioninFile=0;
char word[100]="";
char allWords[10000]="";
string strAllWords="", oldKey="";
int totalCharacters=0;
int totalCharactersBeforeKey=0;
char allWordsBeforeKey=0;
string strAllWordsBeforeKey="";
while(!file.eof()) //This loop will collect all words from file
{
file >> word;
strcat(word," ");
strcat(allWords,word);
}
strAllWords=allWords;
totalCharacters=strAllWords.size();
while(i != totalCharacters)
{
if(strAllWords[i] == '%')
{
flag=1;
}
i++;
}
if(flag==0)
{
cout<<"Your file is not protected. First protect it using option 1. \n\n";
menu(file, filename);
}
i=0;
if(flag) //If the key is already generated in the file
{
while(strAllWords[i] != '%')
{
allWordsBeforeKey=allWordsBeforeKey+strAllWords[i];
i++;
}
strAllWordsBeforeKey=allWordsBeforeKey;
i=0;
while(strAllWords[i] == '%') //To note that where old key is located in the file
{
keyPositioninFile=i;
}
for(int j=keyPositioninFile; j<=strAllWordsBeforeKey.size(); j++)
{
oldKey[j]=strAllWordsBeforeKey[j];
}
totalCharactersBeforeKey=strAllWordsBeforeKey.size();
reFindASCII(allWordsBeforeKey, totalCharactersBeforeKey, file, filename, oldKey);
}
}
void menu(std::ifstream &file, string filename)
{
int option=0;
cout<<"Operations on text file: \n";
cout<<"1. Protect my text file \n";
cout<<"2. Check security of my text file \n\n";
cout<<"Enter your choice (1/2): ";
cin>>option;
if(option==1)
protectFile(file, filename);
else if(option==2)
checkFile(file, filename);
else
cout<<"Error: option selection is invalid";
}
void openFile(string filename)
{
ifstream theFile;
theFile.open(filename);
if(!theFile){
cout<<"Cant open input file named "<<filename;
system("pause>nul");
exit(1);
}
menu(theFile, filename);
}
void findSize(std::ifstream &file, string filename)
{
char word[100]="";
char allWords[10000]="";
string strAllWords="";
int totalCharacters=0;
while(!file.eof())
{
file >> word;
cout<<word<<" ";
strcat(word," ");
strcat(allWords,word);
}
strAllWords=allWords;
totalCharacters = strAllWords.size();
findASCII(allWords, totalCharacters, file, filename);
}
void findASCII(char allWords[], int size, std::ifstream &file, string filename)
{
int i=0, totalASCIIofChars=0;
while(i != size)
{
totalASCIIofChars = totalASCIIofChars + allWords[i];
i++;
}
key(size, totalASCIIofChars, file, filename);
}
void reFindASCII(char allWordsBeforeKey, int size, std::ifstream &file, string filename, string oldKey)
{
int i=0, totalASCIIofCharsBeforeKey=0;
string strAllWordsBeforeKey=&allWordsBeforeKey;
while(i != size)
{
totalASCIIofCharsBeforeKey = totalASCIIofCharsBeforeKey + strAllWordsBeforeKey[i];
i++;
}
reKey(size, totalASCIIofCharsBeforeKey, file, filename, oldKey);
}
void key(int size, char ASCII, std::ifstream &file, string filename)
{
string key="";
char cSize = size;
key=cSize+ASCII;
// cout<<"Key is: "<<size; /////////////////PROBLEM HERE//////////
key='%'+key;
// strcat(&key, "%");
putKeyinFile(filename, key, file);
cout<<"Your file is protected now. \n";
system("pause");
}
void protectFile(std::ifstream &file, string filename)
{
int i=0;
char word[100]="";
char allWords[10000]="";
string strAllWords="";
while(!file.eof())
{
file >> word;
strcat(word," ");
strcat(allWords,word);
}
strAllWords=allWords;
while(i != strAllWords.size())
{
if(allWords[i] == '%')
{
cout<<"File is already protected, first protect it by using option 1";
menu(file, filename);
}
i++;
}
findSize(file, filename);
}
void putKeyinFile(string fName, string key, std::ifstream &file)
{
ofstream oFile;
oFile.open(fName, ios::app);
oFile<<key;
oFile.close();
file.close();
}
int main()
{
// char fName[30];
string fName;
cout<<"Enter the name of text file in current directory: ";
cin>>fName;
openFile(fName);
}
【问题讨论】:
-
TL;博士。请创建一个MCVE。注意:阅读标签说明! C 和 C++ 是不同的语言。您的代码不是 C 代码!
-
使用调试器检查错误在哪里。
-
您提出的要求没有描述文字。为什么要分词?
-
对于多字符项目坚持使用
std::string或char *。不要混合它们。我推荐使用std::string。