[英]how to reading .raw file in c++
void solution3(){
char str[100];
int height, width;
printf("가로와 세로 길이를 입력 : ");
scanf("%d %d", &width, &height);
FILE* fp = fopen("lena_256x256.raw", "rb");
//2dynimic
unsigned char **p = new unsigned char*[height];
for(int i = 0; i < height; i++) *(p + i) = new unsigned char[width];
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++)
fread((void*)p[i][j], sizeof(unsigned char), height * width, fp);
}
//print
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++)
printf("%4d ", p[i][j]);
}
//del dynimic
for(int i = 0; i < height; i ++) delete[] p[i];
delete[] p;
fclose(fp);
}
void main(){
solution3();
}
Error message is "string != NULL" why cant read .raw file? 错误消息为“ string!= NULL”,为什么无法读取.raw文件? and I try p[i][j]
fix (void*)p[i][j]
. 我尝试p[i][j]
fix (void*)p[i][j]
。 but can not. 但不能。
I want read .raw and print info about .raw light 我想阅读.raw并打印有关.raw light的信息
The code has undefined behavior here: 该代码在这里具有未定义的行为:
unsigned char **p = new unsigned char*[height]; .... fread((void*)p[i][j], sizeof(unsigned char), height * width, fp);
p
is a pointer to pointer of char. p
是char的指针。 Which means that p[i][j]
is a char, which the code casts to (void*)
. 这意味着p[i][j]
是一个char,代码将其强制转换为(void*)
。 This is an invalid address which fread
tries to write to. 这是fread
尝试写入的无效地址。
It is likely that you want: 您可能想要:
for(int i = 0; i < height; i++){
if (fread((void*)p[i], 1, width, fp) != width)
{
manage the error condition
}
}
Aside of that, it is not recommended to work directly with new/delete, and std::vector would be safer in this case. 除此之外,不建议直接使用new / delete,在这种情况下std :: vector会更安全。 Also, void main()
is wrong and should be int main()
instead. 同样, void main()
是错误的,应该改为int main()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.