1

ファイル(実際には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 があると仮定しています。それをスキップしたり、後で読み続けたりするにはどうすればよいですか? 再度、感謝します

4

2 に答える 2

3

ファイルをバイナリ モードで開き、次を使用しますget()

    ifstream streamLecture(fileName, std::ios::binary);
    while (streamLecture.get(charLu)) {
        cout<< hex << setw(2) << setfill('0') << short(charLu) << ' ';
    }
于 2013-10-17T04:17:57.980 に答える
0

使用する

while(streamLecture >> charLu){
    cout<< hex << setw(2) << setfill('0') << short(charLu) << ' ';
}
于 2013-10-17T03:24:08.200 に答える