【发布时间】:2024-01-20 02:25:01
【问题描述】:
我正在开发文本文件解码器和编码器,它们处理两个不同的文本文件。解码器在编码消息下方打印解码消息,但它也打印一堆其他内容。我该如何解决这个问题
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
ifstream fin; // input file
string line;
ofstream fout;
//open output file
fout.open("secret.txt", ios::app);
if (!fout.good()) throw "I/O error";
// open input file
fin.open("secret.txt");
if (!fin.good()) throw "I/O error";
// read input file, decode, display to console
while (fin.good()) {
getline(fin, line);
for (int i = 0; i < line.length(); i++) // for each char in the string...
line[i]--; // bump the ASCII code down by 1
fout << line << endl; // display on screen
}
// close file
fin.close();
return 0;
}
编码器读取的文本文件
Uftujoh234
如果mmp!nz!obnf!jt!cpc
Dmptfe!
乌夫图乔
解码为
测试123
你好,我叫鲍勃
关闭
测试
这是它也在文本文件中打印的所有额外内容
Sdrshmf012
Gdkknlxm`ldhrana
Bknrdc
Sdrshmf
Rcqrgle/01
Fcjjmkwl_kcgq`m`
Ajmqcb
Rcqrgle
Qbpqfkd./0
Ebiiljvk^jbfp_l_
@ilpba
Qbpqfkd
Paopejc-./
Dahhkiuj]iaeo^k^
?hkoa`
Paopejc
O`nodib,-.
C`ggjhti\h`dn]j]
>gjn`_
O`nodib
N_mncha+,-
B_ffigsh[g_cm\i\
=fim_^
N_mncha
M^lmbg`*+,
A^eeh
【问题讨论】:
标签: c++ encryption iostream fstream text-files