将 0A 替换为 \n

发布于 2024-08-16 23:09:00 字数 960 浏览 2 评论 0原文

我当时正在开始开发一个简单的十六进制编辑器(当时只能读取)。我想用 OA 替换 "\n",我正在尝试使用以下代码:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main() {
   ifstream infile;
   int crtchar = (int)infile.get();
   infile.open("test.txt", ifstream::in);
   while(infile.good())
   {
      if(crtchar != 0xA)
         cout << hex << setfill('0') << setw(2) << crtchar << ":";
      else
         cout << endl;
   }
   cout << "\n=====================================\n";
   infile.close();
   return 0;
}

它编译时没有错误,但是当我尝试执行它时,我什么也没得到:

C:\Documents and Settings\Nathan Campos\Desktop>hex

========================================

C:\Documents and Settings\Nathan Campos\Desktop>

这是在我添加用 OA 替换 \n 的功能之后发生的,因为之前它工作得很好。 出了什么问题?

I'm at the time beginning the development of a simple hex editor(that only reads at the time). I want to substitute OA for "\n", I'm trying with this code:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main() {
   ifstream infile;
   int crtchar = (int)infile.get();
   infile.open("test.txt", ifstream::in);
   while(infile.good())
   {
      if(crtchar != 0xA)
         cout << hex << setfill('0') << setw(2) << crtchar << ":";
      else
         cout << endl;
   }
   cout << "\n=====================================\n";
   infile.close();
   return 0;
}

It compiles without errors, but when I try to execute it, I just got nothing:

C:\Documents and Settings\Nathan Campos\Desktop>hex

=====================================

C:\Documents and Settings\Nathan Campos\Desktop>

This is happening just after I've added the feature to substitute OA for \n, because before it was working very nice. What is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

送你一个梦 2024-08-23 23:09:00

您意识到您只读取一个字符一次,并且在打开文件之前,那?

You realize that you are only reading a character once, and before even opening the file, at that?

最佳男配角 2024-08-23 23:09:00

叹。您在打开文件之前尝试读取该文件。

Sigh. You try to read the file before you open it.

吝吻 2024-08-23 23:09:00

您是否应该首先open(...)您的文件,然后尝试从中get()
另外,您不应该在 while 循环中执行更多 get() 操作吗

Shouldn't you first open(...) your file and then try to get() from it?
Also, shouldn't you do more get()'s inside your while loop

回心转意 2024-08-23 23:09:00

int crtchar = (int)infile.get(); 放入 while(infile.good()) 中并尝试一下。

Put int crtchar = (int)infile.get(); inside a while(infile.good()) and give it a try.

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