我剛開始開發一個簡單的十六進制編輯器(當時只讀)。我想替換OA
爲"\n"
,我想用這個代碼:代替0A對於 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>十六進制
==================================== =
C:\ Documents and Settings \ Nathan Campos \ Desktop>
這是發生在我添加了功能以替代OA
爲\n
之後發生的,因爲它在工作之前非常好。 有什麼不對?
你確定你運行的是完全相同的程序,除了''\ n''的0xA開關嗎? EOL翻譯是否咬你?如果你確實爲這兩者獲得了不同的行爲,你是否嘗試過打印''\ n''的整數值?爲什麼你甚至希望避免使用更易讀(更便攜)的''n''來支持'0xA'? – 2010-01-06 12:34:16