c++文本解码器解码超出要求

发布于 2024-12-21 00:53:04 字数 1373 浏览 1 评论 0原文

我正在开发文本文件解码器和编码器,它们使用两个不同的文本文件。解码器在编码消息下方打印解码消息,但它也打印一堆其他内容。我该如何解决这个问题,

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐籹人 2024-12-28 00:53:04

您看到的额外数据实际上是解码“secret.txt”中的数据的有效输出。

我不确定这是否是您想要的,但是您是否知道每次运行应用程序时都在读取和写入同一个文件?

您将向文件附加越来越多的“解码”数据,因此您将获得所指的额外输出。


另外,您的 while 循环存在问题。

fin.good () 将保持为真,直到在 fin 内部设置一些错误位,尽管它会进入循环一次太多,因为您应该检查调用 getline (fin, ...) 后立即显示流的状态。

当前读取将失败,但您仍将处理“未读”数据。


std::getline 将返回流对象,并且由于 std::istream (以及 std::ostream)可以隐式地转换为布尔值以检查其当前状态,您应该将其用作循环条件。

将您的循环更改为如下所示,看看是否可以解决您的问题。

  while (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
  }

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 of fin, though it will enter the loop one time too much since you should check to state of the stream immediately after your call to getline (fin, ...).

Currently the reading will fail but you will still process the "unread" data.


std::getline will return the stream object, and since a std::istream (as well as std::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.

  while (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
  }
辞取 2024-12-28 00:53:04

额外的东西并不是额外的。您正在将数据写入正在读取的同一个文件中,因此您要做的是:

  1. write line
  2. read line

您正在重新编码已经编码的数据。

The extra stuff isn't extra. You are writing data into the same file you are reading, so what you do is:

  1. write line
  2. read line

You are renencoding the data you already encoded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文