蚌埠住了,自己寫bmp檔案的讀寫操作時會報錯,錯誤陳述句在fopen這一行上,以下是代碼:
int Laplace[3][3] =
{
{ -1, -1, -1 },
{ -1, 8, -1 },
{ -1, -1, -1 }
};
int main()
{
char* fpath = "E:\\pic_test\\pic_test_out\\3.bmp";
char* fdstpath = "E:\\pic_test\\pic_test_out\\7.bmp";
BMP bitmap, bitmapout;
bmpRead(bitmap, fpath);
PicGrad(bitmap, Laplace, bitmapout);
bmpWrite(bitmapout, fdstpath);
delete[] bitmap.data;
delete[] bitmapout.data;
return 0;
}
void bmpWrite(BMP bitmap, const char *bmpPath)
{
FILE *file = fopen(bmpPath, "wb");
if (NULL == file)
return;
int iByteLength = 0;
int colorsize = 0;
if (8 == bitmap.mCount)
{
iByteLength = (bitmap.mWidth + 3) / 4 * 4;
colorsize = 1024;
}
else if (24 == bitmap.mCount)
{
iByteLength = (bitmap.mWidth * 3 + 3) / 4 * 4;
}
else
{
iByteLength = (bitmap.mWidth *bitmap.mCount / 3 + 3) / 4 * 4;
}
BITMAPFILEHEADER bmphd;
bmphd.bfType = 0x4D42;
bmphd.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER) + colorsize;
bmphd.bfSize = bmphd.bfOffBits + iByteLength*bitmap.mHeight;
bmphd.bfReserved1 = 0;
bmphd.bfReserved2 = 0;
BITMAPINFOHEADER bmpin;
bmpin.biBitCount = bitmap.mCount;
bmpin.biClrImportant = 0;
bmpin.biClrUsed = 0;
bmpin.biCompression = 0;
bmpin.biHeight = bitmap.mHeight;
bmpin.biPlanes = 1;
bmpin.biSize = sizeof(BITMAPINFOHEADER);
bmpin.biSizeImage = 0;
bmpin.biWidth = bitmap.mWidth;
bmpin.biXPelsPerMeter = 0;
bmpin.biYPelsPerMeter = 0;
fwrite(&bmphd, sizeof(BITMAPFILEHEADER), 1, file);
fwrite(&bmpin, sizeof(BITMAPINFOHEADER), 1, file);
if (bitmap.mCount == 8)
{
unsigned char uchPalette[256 * 4];
for (int j = 0; j < 256; j++)
{
uchPalette[j * 4] = (unsigned char)j;
uchPalette[j * 4 + 1] = (unsigned char)j;
uchPalette[j * 4 + 2] = (unsigned char)j;
uchPalette[j * 4 + 3] = 0;
}
fwrite(uchPalette, sizeof(RGBQUAD), 256, file);
}
fwrite(bitmap.data, iByteLength, bitmap.mHeight, file);
fclose(file);
}
錯誤就在bmpwrite()函式的第一行...進到函式內部一直到這個地方出問題:

不太清楚問題出在哪里了,求大大們解答!!
uj5u.com熱心網友回復:
已經解決了,但是無滿意結帖不退分,不想浪費分數,隨便來幾個人我把分散一下uj5u.com熱心網友回復:
蹭分的來了[face]emoji:019.png[/face]uj5u.com熱心網友回復:
應該是檔案路徑問題吧,可以通過strerror(errno);看一下fopen打開失敗的原因。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/284222.html
標籤:C語言
