2011-11-20 53 views
1

我正在開發一個程序,用凱撒密碼對.txt文件進行加密和解密。 (鍵盤輸入是可選的)我做了一個努力,但很難過。凱撒密碼/文件輸入不起作用

我現在所擁有的是一個基本的或我猜想已經有點精心設計的菜單結構。 我已經到了我應該從文件中調用信息的地方,它只是調用垃圾或垃圾是一種方式或另一種方式。

這是我的代碼,它運行到輸入部分,然後在輸入後結束。

其選項1選項1然後任意數字,然後它運行文件輸入,然後結束。

在此先感謝。我幾乎到處都找,並嘗試了3或4種不同的方式都無濟於事。

所以我想知道的是我應該怎麼做才能修復文件輸入和建議如何採取該數組或字符串如何建議並通過移位值更改每個字符的ascii值。

提前致謝。

#include <iostream> 
#include <fstream> 
#include <cmath> 
#include <cctype> 
#include <cstring> 
#include <string> 
#include <sstream> 
#include <dos.h> 
#include <stdio.h> 
#include <windows.h> 
#include <cstdlib> 

using namespace std; 

int decryptce(); 
int encryptcefile(); 
int encryptce(); 
int outputce(); 
int encmenu(); 
int decmenu(); 

int main() 
{ 
    int t; 
     //main menu 
    cout << "-----------------------------------------------------"<<endl; 
    cout << "This is a Caesar Cipher, please select [1] or [2]" << endl; 
    cout << "-----------------------------------------------------"<<endl; 
    cout << "[1] Encrypt"<<endl; 
    cout << "[2] Decrypt"<<endl; 
    cout << "What is your choice:\t"; 
    cin >> t; 
    cout << "\n-----------------------------------------------------"<<endl; 

    // menu switch statement 
    switch (t) 
    { 
     case 1: 
     { 
      cout << "\n running encryption:\n\n"; 
      encmenu(); 
      break; 

     } 
     case 2: 
     { 
      cout << "\n running decryption:\n\n"; 
      decmenu(); 
      break; 
     } 
     default: 
     { 
      cout<< "Thats not a 1 or 2"<<endl; 
      cout<< "Relaunch the program"<<endl; 
      system ("pause"); 
      return 0; 
     } 
    } 

    return 0; 
} 

int encmenu() 
{ 

    int t; 
    cout << "-----------------------------------------------------"<<endl; 
    cout << "You selected: Encrypt with Caesar Cypher"<<endl; 
    cout << "-----------------------------------------------------"<<endl; 
    cout <<"Do you want to enter from a file or the keyboard?"<<endl; 
    cout <<"Enter [1] From file, or [2] From Keyboard"<<endl; 
    cout <<"Run option:\t"; 
    cin >> t; 
    cout << "\n-----------------------------------------------------"<<endl; 
    // encrypt menu switch 

    switch (t) 
    { 
     case 1: 
     { 
      encryptcefile(); 
      break; 
     } 
     case 2: 
     { 
      encryptce(); 
      break; 
     } 
     default: 
     { 
      cout<< "Thats not a 1 or 2"<<endl; 
      cout<< "Relaunch the Program"<<endl; 
      system ("pause"); 
      return 0; 
     } 
    } 
    return 0; 
} 

// decrypt main menu 
int decmenu() 
{ 
    int h; 
    cout << "-----------------------------------------------------"<<endl; 
    cout << "You selected: Decrypt with Caesar Cypher"<<endl; 
    cout << "-----------------------------------------------------"<<endl; 
    cout << "Reading from Data.txt in main file directory:"; 
    cout << "\n"<<endl; 
    cout << "Ofset is:\t"; 
    cin >> h; 
    cout << "\n-----------------------------------------------------"<<endl; 
    return h; 
} 

int decryptce() 
{ 
    return 0; 
} 


int encryptcefile() 
{ 
    // for figuring out what to do with negatives 
    /* 
    letter = x; 
    letter = (letter + shift + 26) % 26; 
    // add 26 in case the shift is negative 
    letter += x; // back to ascii code 

    */ 
    char c,i; 

    int num=0; 
    int shift; 
    ofstream ofile; 
    ifstream ifile; 
    ifile.open("data.txt"); 
    ofile.open("Encrypted.txt"); 
    if(!ifile) 
    { 
     cout << "Error Opening File" << endl; 
     return 0; 
    } 
    if(!ofile) 
    { 
     cout << "Error Opening File" << endl; 
     return 0; 
    } 

    while (ifile.good()) 
    { 
     c = ifile.get(); 
     if (c=='\n')num++; 
     { 
      cout << "Is: " << num << " Long. \r"; 
      Sleep(1); 
      fflush (stdin); 
     } 
    } 

    cout << "File is: " << num << " characters Long.\n"<<endl; 
    cout << "\n-----------------------------------------------------"<<endl; 
    cout << "What is the shift for the encryption:\t"; 

    // ---------------------working here------------------- 
    cin >> shift; 

    const int xx = num+1; 
    char *arraye; 
    char *arrayce; 
    int j; 

    arraye = new char [xx]; 
    arrayce = new char [xx]; 
    arrayce[xx]; 
    ifile.read(arrayce,xx); 

    for(j=0;j<xx;j++) 
    { 
     arraye[j]=arrayce[j]; 
     cout << arrayce[j]<< endl; 
    } 

    return 0; 
} 

int encryptce() 
{ 
    return 0; 
} 

int outputce() 
{ 
    return 0; 
} 
+1

請不要解析整個代碼,只有相關的部分,最好是*最低工作對不起*例子。你發佈的大部分代碼與問題完全無關,而且令人分心。 –

回答

3

這通常是解析文件的不好方法。 ifile.good()將是真實的,直到您嘗試讀取過去文件的結尾,這意味着它將讀取一次太多次。

while (ifile.good()) 
    { 
     c = ifile.get(); 
     if (c=='\n')num++; 
     { 
      cout << "Is: " << num << " Long. \r"; 
      Sleep(1); 
      fflush (stdin); 
     } 
    } 

if聲明在那裏是錯誤的。現在,當條件爲真時,它執行num++;,但塊部分在每個迭代中都執行while。 您在num中存儲換行符('\ n')的字符數,並在代碼中存儲「文件長度爲num」字符。所以我不確定您是要計算行數還是確定文件的大小。我不知道fflush(stdin);的目的是什麼。

更簡單,更清潔,更正確的方法是將讀取語句置於while條件中,這樣,當讀取失敗時不會進入循環。所以,如果你想逐行讀取文件中的行,做這樣的事情:

std::vector<std::string> file_by_lines; // pick a better name :) 
std::string line; 
while (std::getline(ifile, line)) 
{ 
    num += line.length(); 
    file_by_lines.push_back(line); 
} 
std::cout << "File is " << num << " characters Long." << std::endl; 

在那裏,你有存儲在一個矢量文件,有沒有必要爲自己的文件動態分配空間了,矢量照顧這一點。

另外,如果你需要字符閱讀字符:

// get the size of the file 

ifile.seekg (0, ios::end); 
size_t length = ifile.tellg(); 
ifile.seekg (0, ios::beg); // move read pointer back to the beggining 

char* buffer = new char[length]; 
ifile.read(buffer, length); 

// free the allocated memory once you're done. 

delete [] buffer; 

一件事。將來,請考慮縮減您的代碼以解決實際問題。當你只有(在你的情況下)輸入有問題時,你不需要發佈你的整個代碼。 200行格式不正確的代碼使得你不太可能讓人們來幫助你。閱讀短,自成一體,可編譯例子在這裏:http://sscce.org/

希望幫助,爲我的累贅英語:)

+0

謝謝你,我非常感謝你的幫助。我知道我的代碼有一些主要缺陷。我記得下次我發佈,再次感謝。 –