c++文本解码器解码超出要求
我正在开发文本文件解码器和编码器,它们使用两个不同的文本文件。解码器在编码消息下方打印解码消息,但它也打印一堆其他内容。我该如何解决这个问题,
#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
Ifmmp!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
Im working on a text file decoder along with an encoder, they work off of two different text files. The decoder prints the decoded message underneath the encoded message but it also prints a bunch of other stuff as well. How do i fix this
#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;
}
the text file from the encoder reads
Uftujoh234
Ifmmp!nz!obnf!jt!cpc
Dmptfe!
Uftujoh
which decodes to
Testing123
Hello my name is bob
Closed
Testing
this is all the extra stuff it also prints in the text file
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您看到的额外数据实际上是解码“secret.txt”中的数据的有效输出。
我不确定这是否是您想要的,但是您是否知道每次运行应用程序时都在读取和写入同一个文件?
您将向文件附加越来越多的“解码”数据,因此您将获得所指的额外输出。
另外,您的
while
循环存在问题。fin.good ()
将保持为真,直到在fin
内部设置一些错误位,尽管它会进入循环一次太多,因为您应该检查调用getline (fin, ...)
后立即显示流的状态。当前读取将失败,但您仍将处理“未读”数据。
std::getline
将返回流对象,并且由于std::istream
(以及std::ostream
)可以隐式地转换为布尔值以检查其当前状态,您应该将其用作循环条件。将您的循环更改为如下所示,看看是否可以解决您的问题。
The extra data you see is actually valid output from decoding the data in
"secret.txt"
.I'm not sure if this is what you want, but are you aware that you are reading and writing to the same file each time you run your application?
You'll append more and more "decoded" data to the file, and therefore you get the extra output you are referring to.
Also, there is an issue with your
while
-loop.fin.good ()
will remain true until some of the error bits has been set inside offin
, though it will enter the loop one time too much since you should check to state of the stream immediately after your call togetline (fin, ...)
.Currently the reading will fail but you will still process the "unread" data.
std::getline
will return the stream object, and since astd::istream
(as well asstd::ostream
) implicitly can be converted to a boolean to check it's current state you should use that as your loop-condition.Change your loop into something as the below and see if that solves your problem.
额外的东西并不是额外的。您正在将数据写入正在读取的同一个文件中,因此您要做的是:
您正在重新编码已经编码的数据。
The extra stuff isn't extra. You are writing data into the same file you are reading, so what you do is:
You are renencoding the data you already encoded.