我目前正在一个程序,我可以在一个文本文件(称为plaintext.txt),以及一个密钥文件中替换字母,并创建一个密文当我运行一个命令将它们混合在一起。工作代码如下所示:std :: stringstream输出不工作相同std ::字符串
string text;
string cipherAlphabet;
string text = "hello";
string cipherAlphabet = "yhkqgvxfoluapwmtzecjdbsnri";
string cipherText;
string plainText;
bool encipherResult = Encipher(text, cipherAlphabet, cipherText);
bool decipherResult = Decipher(cipherText, cipherAlphabet, plainText);
cout << cipherText;
cout << plainText;
输出上面的代码将低于
fgaam
hello
不过,我想我的“文本”和“cipherAlphabet”转换成在那里我得到的字符串他们两人通过不同的文本文件。
string text;
string cipherAlphabet;
std::ifstream u("plaintext.txt"); //getting content from plainfile.txt, string is text
std::stringstream plaintext;
plaintext << u.rdbuf();
text = plaintext.str(); //to get text
std::ifstream t("keyfile.txt"); //getting content from keyfile.txt, string is cipherAlphabet
std::stringstream buffer;
buffer << t.rdbuf();
cipherAlphabet = buffer.str(); //get cipherAlphabet;*/
string cipherText;
string plainText;
bool encipherResult = Encipher(text, cipherAlphabet, cipherText);
bool decipherResult = Decipher(cipherText, cipherAlphabet, plainText);
cout << cipherText;
cout << plainText;
但是,如果我这样做,我得到没有输出,没有错误?有没有人可以帮我解决这个问题?谢谢!!
在从中读取数据以查看状态是否仍然良好之前,请务必检查“if(t)”。 –
您没有阅读文件。只需将文件读入std :: string并完成它即可。你可以谷歌如何。 –
@AnonMail OP实际上是使用'rdbuf()'读取文件。 –