将 0A 替换为 \n
我当时正在开始开发一个简单的十六进制编辑器(当时只能读取)。我想用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您意识到您只读取一个字符一次,并且在打开文件之前,那?
You realize that you are only reading a character once, and before even opening the file, at that?
叹。您在打开文件之前尝试读取该文件。
Sigh. You try to read the file before you open it.
您是否应该首先
open(...)
您的文件,然后尝试从中get()
?另外,您不应该在 while 循环中执行更多
get()
操作吗Shouldn't you first
open(...)
your file and then try toget()
from it?Also, shouldn't you do more
get()
's inside your while loop将
int crtchar = (int)infile.get();
放入while(infile.good())
中并尝试一下。Put
int crtchar = (int)infile.get();
inside awhile(infile.good())
and give it a try.