凯撒密码/文件输入无效

时间:2011-11-20 09:55:41

标签: c++

我正在开发一个用Caesar密码加密和解密.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 个答案:

答案 0 :(得分: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);的目的是什么。

更简单,更清晰,更正确的方法是将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;

在那里,你已经将文件存储在一个向量中,不再需要自己为文件动态分配空间了,向量就可以解决这个问题。

或者,如果您需要通过char读取char:

// 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/

,了解简短,自包含,可编辑的示例

希望有帮助,并为我繁琐的英语抱歉:)

相关问题