为什么我的文件字符没有读取而密钥没有生成?

时间:2015-07-08 18:38:59

标签: c++ file size ascii

这是我的计划。目的是找到没有。文件中的字符所有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);


}

1 个答案:

答案 0 :(得分:0)

这是您的程序的较小版本,可能使用您不了解的功能:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdint>
#include <sstream>

using std::string;
using std::ifstream;
using std::cout;
using std::endl;
using std::cerr;
using std::cin;
using std::uint64_t;
using std::ostringstream;

void Pause(void)
{
  cout << "\nPaused. Press Enter to continue.\n";
  cin.ignore(100000, '\n');
}

int main(void)
{
  ifstream  input("my_datafile.txt", std::ios::binary) // prevent translations
  if (!input)
  {
    cerr << "Error opening \"my_datafile.txt\" as input.\n";
    Pause();
    return EXIT_FAILURE;
  }
  uint64_t chars_in_file = 0U;
  uint64_t ascii_sum = 0U;
  static const unsigned int BUFFER_SIZE = 1024 * 8;
  static char buffer[BUFFER_SIZE];
  while (input.read(buffer, BUFFER_SIZE)
  {
    unsigned int bytes_read = input.gcount();
    chars_in_file += bytes_read;
    for (unsigned int i = 0; i < bytes_read; ++i)
    {
      ascii_sum += buffer[i];
    }
  }
  ostringstream formatting_stream;
  formatting_stream << chars_in_file << ascii_sum;
  const std::string key = formatting_stream.str();
  cout << "The key is: " << key << "\n";
  Pause();
  return 0;
}

我认为您的代码中不需要很多东西。