所以我正在研究一个应该在pgm/ppm文件中读取并最终修改它的类。但是,我仍在努力读取数据。我目前的目标是为二进制数据创建存储空间。下面是我应该这样做的函数的代码:
uchar* pxm::newimg(int nrows, int ncols, int bpp)
{
uchar *newimage = new uchar[nrows * ncols * bpp];
//uchar defined as binary data previously
return newimage;
}
然后,我在"读取"函数中使用它作为:
img = newimg(nrows, ncols, bpp); //img declared in private section of class
ws(myfile); //reads the newline
myfile.read((char *) img[0], nrows * ncols * bpp);
//nrows, ncols, and bpp private member variables in class
myfile.close();
遗憾的是,当此代码编译时,它会在运行时产生分段错误。我相当确定我的问题出在 newimg 函数中,因为我已经进行了检查以确保我正确读取文件并正确设置 nrows、ncols 和 bpp,以及 magicid。因此,我认为我没有正确初始化数组数据结构。如果有人有任何建议,将不胜感激。谢谢截塔附言如果您需要有关我的代码的更多详细信息,请请求它,我将在帖子中添加更多内容。
(char *) img[0]
应该是img
,或者(char *) img
。