我正在製作一個C++程序,以便能夠打開一個.bmp圖像,然後能夠將它放入二維數組中。現在,我有這樣的代碼:無法讀取整個文件
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "Image.h"
using namespace std;
struct colour{
int red;
int green;
int blue;
};
Image::Image(string location){
fstream stream;
string tempStr;
stringstream strstr;
stream.open(location);
string completeStr;
while(!stream.eof()){
getline(stream, tempStr);
completeStr.append(tempStr);
}
cout << endl << completeStr;
Image::length = completeStr[0x13]*256 + completeStr[0x12];
Image::width = completeStr[0x17]*256 + completeStr[0x16];
cout << Image::length;
cout << Image::width;
cout << completeStr.length();
int hexInt;
int x = 0x36;
while(x < completeStr.length()){
strstr << noskipws << completeStr[x];
cout << x << ": ";
hexInt = strstr.get();
cout << hex << hexInt << " ";
if((x + 1)%3 == 0){
cout << endl;
}
x++;
}
}
,如果我現在對我的256×256的測試文件運行這個將打印精細,直到它到達0x36E它給出了一個錯誤/不走的更遠。發生這種情況是因爲completeStr字符串不會收到bmp文件中的所有數據。爲什麼不能讀取bmp文件中的所有行?
''時爲[車和錯誤(HTTP(EOF()!)://計算器。 COM /問題/ 5605125 /爲什麼 - 是 - iostreameof-內,一個循環條件考慮的,是錯誤的)。如果這是您的問題,我不會感到驚訝。 – chris 2013-04-05 17:32:18
您可能會考慮將位圖讀取爲二進制文件,而不是線條集合。 – 2013-04-05 17:37:24
[讀取文件到C++中的字符串]可能的重複(http://stackoverflow.com/questions/3286822/reading-a-file-to-a-string-in-c) – 2013-04-05 17:45:20