客户解密程序c ++麻烦阅读

时间:2016-10-12 18:36:31

标签: c++ arrays encryption dynamic decode

所以我有一个自定义解密程序,它读入输入文件并根据输入的密钥对其进行解密。 文本文件:

23
11

Java 2 linux 3 fear 0 pool 2 do 0 red 1 lock. 1 I 0 random 2 computers, 0 not 0 the 0 open 2 car! 2 C, 0 lack 0 of 0 dog 1 green 2 C++ 0 bottle 2 wrong, 2 them. 0

5 1 10 21 9 6 21 11 13 16 20

此文件表示为:

[Number of Words]
[Number of Keys]

[Word] [Jump] [Word] [Jump] ... [Word] [Jump]   

[Key] [Key] ... [Key] 

我已经设法让程序读取键的数量和单词的数量,但是我无法读取它们旁边的单词和数字以及最后的数字并将它们作为键注册。这就是我到目前为止所做的:

#include <iostream>
#include <fstream>

using namespace std;

struct pieces {

char word;
int jump;

} ;

 // Main Function
int main ()
{
// declare variables
int keyCount = 1;
int wordCount = 8;
int wordAmount[8];
int keyAmount[8];
pieces cipher[5];
char decoded[20][20];
char filename[10];
int keys[keyCount];
char tArray[20][20];
ifstream inData;

  //prompt user for input file

cout << " Enter file name: ";
cin >> filename;

inData.open(filename);

if(inData.is_open());
{

    // read list of names into array

    for ( int i = 0; i < keyCount; ++i){           

    inData >> wordAmount[i] >> keyAmount[i];

        for(int j = 0; j < wordCount; j++){

        inData >> cipher[j].word >> cipher[j].jump;

        }

    }

cout << " Key Count: " << keyCount << "\n";

    // print out 

        for ( int i = 0; i < keyCount; ++i){

    cout << " KeyAmount: ";
    cout << keyAmount[i] << "\n";
    cout << " WordAmount: ";
    cout << wordAmount[i] << "\n";

        for(int j = 0; j < wordCount; j++){

        cout << cipher[j].word << " " << cipher[j].jump;

            }

            }

    }

inData.close();

  return 0;
}

我确实尝试将char字作为数组,但后来我得到了一个分段文件。任何建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

恕我直言,如果您使用std::string并重载格式化的输入运算符>>,您的项目会更简单:

struct Word_Jump
{
  std::string word; // Using std::string because it has space for more than one character.
  int jump;
  friend std::istream& operator>>(std::istream& input, Word_Jump& wj);
};

std::istream&
operator>>(std::istream& input, Word_Jump& wj)
{
  input >> wj.word;
  input >> jump;
  return input;
}

您的输入可能如下所示:

std::vector<Word_Jump> database;
for (unsigned int i = 0; i < number_of_words; ++i)
{
  Word_Jump wj;
  my_data_file >> wj;
  database.push_back(wj);
}

同样,您输入阅读键:

std::vector<int> key_database;
for (unsigned int j = 0; j < number_of_keys; ++j)
{
  int k = 0;
  my_data_file >> k;
  key_database.push_back(k);
}

以上是两种可能的阅读方法&amp;跳跃对和键。