【发布时间】:2014-04-23 00:07:53
【问题描述】:
我正在开发一个可以在 C++ 中对文本进行编码然后解码的程序。我正在使用堆栈库。该程序的工作方式是它首先要求您提供一个密码密钥,您手动输入该密钥。然后它会询问文件名,这是一个文本文件。如果它是一个普通的 txt 文件,它会将消息编码为一个新文件并添加一个 .iia 文件扩展名。如果文本文件已经具有 .iia 文件扩展名,那么它会解码消息,只要密码密钥与用于对其进行编码的密钥相同。
我的程序确实进行了编码和解码,但它解码的字符数由temp.size() % cypher.length() 决定,它位于readFileEncode() 函数的while 循环中。我认为这是阻止整个文件被编码然后正确解码的原因。换句话说,从说“example.txt.iia”解码回“example.txt”后的结束文件丢失了原始“example.txt”文件中的大部分文本。我只尝试了cypher.length(),但当然那不会编码或解码任何东西。整个过程由解码和编码的那个参数决定。
我似乎无法找到对任何大小文件中的所有字符进行编码和解码的确切逻辑。以下是执行解码和编码的函数的以下代码:
编辑:使用 WhozCraig 为我编辑的代码:
void readFileEncode(string fileName, stack<char> &text, string cypher)
{
std::ifstream file(fileName, std::ios::in|std::ios::binary);
stack<char> temp;
char ch;
while (file.get(ch))
temp.push(ch ^ cypher[temp.size() % cypher.length()]);
while (!temp.empty())
{
text.push(temp.top());
temp.pop();
}
}
编辑:需要一个堆栈。我将实现我自己的堆栈类,但我试图让它首先与堆栈库一起工作。另外,如果有更好的实现方式,请告诉我。否则,我相信除了让它通过循环对整个文件进行编码和解码之外,这并没有太大的问题。我只是不确定它为什么会停在,有时说 20 个字符或 10 个字符。我知道这与密码的长度有关,所以我相信它在 % (mod) 中。只是不知道如何重写。
编辑:好的,尝试了 WhozCraig 的解决方案,但我没有得到所需的输出,所以现在错误必须在我的主要内容中。这是我的主要代码:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cctype>
#include <stack>
using namespace std;
void readFileEncode(string fileName, stack<char> &text, string cypher);
int main()
{
stack<char> text; // allows me to use stack from standard library
string cypher;
string inputFileName;
string outputFileName;
int position;
cout << "Enter a cypher code" << endl;
cin >> cypher;
cout << "Enter the name of the input file" << endl;
cin >> inputFileName;
position = inputFileName.find(".iia");//checks to see if the input file has the iia extension
if (position > 1){
outputFileName = inputFileName;
outputFileName.erase(position, position + 3);// if input file has the .iia extension it is erased
}
else
//outputFileName.erase(position, position + 3);// remove the .txt extension and
outputFileName = inputFileName + ".iia";// add the .iia extension to file if it does not have it
cout << "Here is the new name of the inputfile " << outputFileName << endl; // shows you that it did actually put the .iia on or erase it depending on the situation
system("pause");
readFileEncode(inputFileName, text, cypher); //calls function
std::ofstream file(outputFileName); // calling function
while (text.size()){// goes through text file
file << text.top();
text.pop(); //clears pop
}
system("pause");
}
基本上,我正在读取 .txt 文件进行加密,然后在文件名上添加 .iia 文件扩展名。然后我回去,输入带有 .iia 扩展名的文件以将其解码。当我将其解码回来时,大约在前十个单词之后是胡言乱语。
@WhozCraig 文件中的空格、换行符或标点符号是否重要?也许有了这里的完整解决方案,您可以指导我找出问题所在。
【问题讨论】:
-
我知道您没有为此使用
queue或deque是有原因的。我只知道... 为什么不仍然让我难以捉摸。 -
需要使用堆栈。我现在正在使用该库,但最终可能会构建自己的堆栈类。
-
你必须为 both 使用堆栈,还是仅仅引用参数就足够了?并不是说它一定会降低你的表现(这可以大大提高,如果有机会我会发布答案)。
-
@WhozCraig 两者都有?我不确定你的意思。您是在谈论仅将指针存储在堆栈上吗?我实际上是在尝试接受 user2445771 的建议,现在就使用字符串。我会尽可能更新该代码。我正在尽我所能扭转这种局面。
标签: c++ file-io encoding stack decoding