我的閱讀功能有問題,只能部分讀取文件。例如,如果我的數組的大小是4 * 4,我的函數只能從 使用C++中的fileio讀取二維數組
像素[0(像素[0] [1],像素[0] [2],像素[0] [3] ,像素[0] [4])
但是,我希望它讀取全部16個像素。我班上的領域是:
unsigned int cols;
unsigned int rows;
uint8_t** pixels;
這裏是我的加載功能:
int Image::load(const char* filename) {
//int count=0;
ifstream inFile(filename);
if (!inFile) {
cerr<<"unable to open file";
return 1;
}
inFile>>cols;
inFile>>rows;
cout<<cols;
cout<<rows;
pixels=new uint8_t*[cols];
for(int i=0;i<cols;i++){
pixels[i]=new uint8_t[rows];
}
for(int i=0;i<cols;i++){
for(int j=0;j<rows;j++){
inFile>>pixels[i][j];
cout<<(uint8_t)(pixels[i][j]);
}
}
//cout<<count;
inFile.close();
return 0;
}
的文件我讀有COLS,行和像素;
4 4
0 0 16
0 1 16
0 2 16
0 3 16
1 0 16
1 1 16
1 2 16
1 3 16
2 0 16
2 1 16
2 2 16
2 3 16
3 0 16
3 1 16
3 2 16
3 3 16
請提供一個可重現的代碼示例以及一個文件示例,它應該有所幫助。 – Fabien
爲什麼不'std :: vector>'? –
genpfault
你應該添加錯誤檢查。另外,只需通過調試器對代碼進行編程就可以快速發現問題。 – hyde