简体   繁体   English

如何为.txt文件字符串编程不同的输出

[英]How to program different outputs for a .txt file string

I seem to be having a problem with my current program. 我当前的程序似乎有问题。

What I am trying to do is create a program that outputs the possible decryptions of a stream of text found in a .txt file. 我正在尝试做的是创建一个程序,该程序输出在.txt文件中找到的文本流的可能解密。

I have created my program up to the following point, but I have run into a problem. 到目前为止,我已经创建了程序,但是遇到了问题。

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(){

    ifstream inputFile;

    string message = " ";
    int lengthOfMessage, counter = 0;
    int ord = 0;
    char newChar;
    inputFile.open("Jumbled Message.txt");

    if(!inputFile){
        cout<<"Input File Cannot Be Found"<<endl;
    }

    while(inputFile)
        getline(inputFile, message);                   
        lengthOfMessage = message.length();

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

            ord = int(message[i]);
            ord += 1;
            newChar = char(ord);

        cout<<"Run"<<counter<<" unjumbled: "<<newChar<<endl;
        message= " ";
        counter ++; 
    }
}

Basically, when the program runs, each individual ASCII value of the text shift once is displayed beside each run amount. 基本上,程序运行时,每个移位量旁边都会显示一次文本移位的每个ASCII值。

For Example: 例如:

Run 0 Unjumbled: \
Run 1 Unjumbled: 2
Run 2 Unjumbled: x

As I have mentioned earlier, I am trying to create different possible message outputs for each run within the program. 如前所述,我正在尝试为程序中的每次运行创建不同的可能的消息输出。

You may want to convert the full string before displaying it. 您可能需要在显示完整字符串之前将其转换。 Then got onto the next line in the file. 然后进入文件的下一行。

Here I have made a sample that convert a line in the file to all possible values and display them, then repeat for next line until end of file. 在这里,我做了一个示例,将文件中的一行转换为所有可能的值并显示它们,然后重复下一行直到文件结束。

#include <iostream>
#include <string>
#include <fstream>

int main()
{
  std::ifstream         inputFile;
  std::string           jumbled_message;
  std::string           unjumbled_message;
  int                   line = 0;

  inputFile.open("Jumbled Messages.txt");
  if (!inputFile)
  {
      std::cerr << "Input File Cannot Be Found" << std::endl;
      return 1;
  }

  while (inputFile)
  {
      getline(inputFile, jumbled_message);
      unjumbled_message.resize(jumbled_message.size());
      for (int shift = 0; shift < 255; shift++)
      {
          for(size_t i = 0; i < jumbled_message.size(); i++)
          {
              unjumbled_message[i] = jumbled_message[i] + shift;
          }
          std::cout<< "Line" << line << " - shift: " << shift
                   << " - unjumbled: "<< unjumbled_message
                   << std::endl;
      }
      unjumbled_message.clear();
      line++;
  }
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM