试图读取文件直到最后(不工作)

时间:2013-10-17 03:16:53

标签: c++

我正在尝试读取文件(实际上是ROM)。问题是我的程序在实际到达文件末尾之前就停止了。这是我写的:

int main(){
cout << "Entter the file to read in HEX: " ;
string fileName;
cin >> fileName;

ifstream streamLecture(fileName);
unsigned char charLu;
while(!streamLecture.eof()){
    streamLecture >> charLu;        
    cout<< hex << setw(2) << setfill('0') << short(charLu) << ' ';
}
streamLecture.close();

cout << endl;
}

这个程序有几行HEX值,但我知道自从我在HEX编辑器程序中读取文件后还有很多内容。

编辑:好的,所以我假设文件中间有一个EOF,我怎么跳过它或继续阅读?再次感谢

2 个答案:

答案 0 :(得分:3)

以二进制模式打开文件,并使用get()

    ifstream streamLecture(fileName, std::ios::binary);
    while (streamLecture.get(charLu)) {
        cout<< hex << setw(2) << setfill('0') << short(charLu) << ' ';
    }

答案 1 :(得分:0)

使用

while(streamLecture >> charLu){
    cout<< hex << setw(2) << setfill('0') << short(charLu) << ' ';
}