The function that contains the fseek statement is:
包含FSEEK语句的函数为:
void readPixelsBMP(FILE* file, struct Pixel** pArr, int width, int height) {
int padding = (4 - (width % 4)) % 4;
for (int row = 0; row < height; ++row) {
for (int col = 0; col < width; ++col) {
unsigned char b,g,r;
fread(&b, sizeof(unsigned char), 1, file);
fread(&g, sizeof(unsigned char), 1, file);
fread(&r, sizeof(unsigned char), 1, file);
pArr[row][col].b = b;
pArr[row][col].g = g;
pArr[row][col].r = r;
}
if (padding != 0) {
fseek(file, padding, SEEK_CUR);
}
}
}
I tried commenting out each line until I found the line that when commented out caused the program to execute with no error. The line containing the fseek statement is the culprit. But I do not understand why fseek is giving error code -1.
我尝试注释掉每一行,直到我发现被注释掉时会导致程序执行没有错误的那一行。包含fSeek语句的行是罪魁祸首。但我不明白为什么fSeek会给出错误代码-1。
更多回答
fseek(file, padding, SEEK_CUR);
does not show how -1 was read. Post code that examines the return value of fseek()
.
FSeek(FILE,PADDING,SEEK_CUR);不显示读取方式。检查fSeek()的返回值的POST代码。
Probably because you've not read my correction to the calculation of padding
in the question you asked previously. (The UV'd answer is wrong, too.) In the calculation you've used, you consider width pixels, not width * 3 for bytes. 59 pixels would require 1 pixel of padding... This is not the point. 59 pixels = 177 bytes, requiring 3 bytes of padding. ... You're probably seeking off the end of the file (or something similar.) Go back and read ALL the comments under your previous post..
可能是因为你还没有读到我对你之前提出的问题中关于填充计算的更正。(紫外线的答案也是错误的。)在您使用的计算中,您考虑的是宽度像素,而不是字节的宽度*3。59个像素需要1个像素的填充...这不是重点。59像素=177字节,需要3字节填充。..。您可能正在寻找文件的末尾(或类似的内容)。回去阅读你上一篇帖子下的所有评论。
When it returns non-zero, it also sets errno
. So use something like if ( fseek( ... ) ) { perror( "fseek" ); exit( 1 ); }
. There's really only two possibilities: Bad inputs (not a valid file handle, or not a valid position), or the file handle doesn't support seeking (pipe or socket)
当它返回非零值时,它还设置errno。因此,请使用if(fSeek(...)){perror(“fSeek”);Exit(1);}这样的命令。实际上只有两种可能:错误的输入(不是有效的文件句柄,或者不是有效的位置),或者文件句柄不支持查找(管道或套接字)
优秀答案推荐
I corrected my mistake in the comments below your previous question. The code you've shown here does not reflect that correction.
我在你上一个问题下面的评论中更正了我的错误。您在此处显示的代码并未反映该更正。
Below might work better for you. (I make no claims that the "bgr" sequence is correct. This is up to the OP.)
下面的内容可能更适合你。(我并不认为“BGR”序列是正确的。这取决于行动。)
void readPixelsBMP( FILE *fp, struct Pixel pArr[], int cols, int rows ) {
int padding = (4 - ((cols*3) % 4)) % 4; // corrected calculation # of padding bytes
for( int r = 0; r < rows; r++ ) { // better variable names
unsigned char bgr[ 3 ]; // 3 at a time...
for( int c = 0; c < cols; c++ ) {
fread( bgr, sizeof bgr, 1, fp ); // 3 at a time...
// Not sure how your 2D array worked in the code presented!
pArr[r * cols + c].b = bgr[0]; // blue
pArr[r * cols + c].g = bgr[1]; // green
pArr[r * cols + c].r = bgr[2]; // red
}
if( padding )
fread( bgr, 1, padding, fp ); // consume padding bytes (if any)
}
}
No need for fseek()
...
不需要fSeek()...
(Credit Chux - Reinstate Monika for corrections. blush)
(Credit Chux-恢复Monika以进行更正。(脸红)
更多回答
我是一名优秀的程序员,十分优秀!