2011-11-20 2 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()이전에을 읽으려고 시도 할 때까지 true가됩니다. 이는 파일을 너무 많이 읽음을 의미합니다.

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);의 목적도 모른다.

더 쉽고 명확하며 더 정확한 방법은 read 문을 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 줄이면 사람들에게 도움을 줄 가능성이 적습니다. 짧은 읽기에 대한, 자기가 포함 된, 여기에 Compilable 예 : http://sscce.org/

희망하는 데 도움이, 바람직하게는 내 성가신 영어 :

+0

감사합니다. 대단히 감사합니다. 내 코드에는 몇 가지 주요 결함이 있음을 알고 있습니다. 다음에 내가 게시 할 때 기억하지 못해 다시 고마워. –

관련 문제