2010-01-06 123 views
0

我剛開始開發一個簡單的十六進制編輯器(當時只讀)。我想替換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之後發生的,因爲它在工作之前非常好。 有什麼不對?

+2

你確定你運行的是完全相同的程序,除了''\ n''的0xA開關嗎? EOL翻譯是否咬你?如果你確實爲這兩者獲得了不同的行爲,你是否嘗試過打印''\ n''的整數值?爲什麼你甚至希望避免使用更易讀(更便攜)的''n''來支持'0xA'? – 2010-01-06 12:34:16

回答

7

你意識到你只是讀取字符一次,並之前甚至打開文件,在那?

3

嘆氣。您嘗試在打開文件之前閱讀該文件。

0

你不應該先把你的文件open(...)從你的文件中再試試get()嗎? 此外,你不應該做你的while循環中更get()

0

int crtchar = (int)infile.get();一個while(infile.good())內,並給它一試。