#define _CRT_SECURE_NO_DEPRECATE#include stdioh#include stdlibh#include sysstath#define PIXEL_SIZE 300 像素大小#define NO_DATA_VALUE -9999 无效像素值int main FILE fp_dem; const char filename = Dkjfxdem
出现 "invalid file size" 的错误提示,说明读取的文件大小与预期不符合,需要检查代码中的文件大小计算是否正确。可以尝试以下修改:
- 将计算文件大小的代码移到文件头信息读取之前,避免计算错误;
- 在文件大小计算之后,添加一个判断语句,检查文件大小是否符合要求;
- 如果文件大小不符合要求,可以尝试使用其他方式读取文件,例如 GDAL 库。
具体修改方式可以参考以下代码示例:
// 计算DEM.tif文件大小
struct stat st;
if (stat(filename, &st) == 0) {
long filesize = st.st_size;
long datasize = filesize - 0x44;
// 检查文件大小是否符合要求
if (datasize % sizeof(float) != 0 || datasize / sizeof(float) != width * height) {
printf("Error: invalid file size!\n");
free(elevation);
fclose(fp_dem);
return 1;
}
// 分配内存空间
elevation = (float*)malloc(datasize);
if (elevation == NULL) {
printf("Error: cannot allocate memory!\n");
fclose(fp_dem);
return 1;
}
// 读取DEM.tif数据
fseek(fp_dem, 0x44, SEEK_SET);
size_t n = fread(elevation, sizeof(float), datasize / sizeof(float), fp_dem);
if (n != datasize / sizeof(float)) {
printf("Error: cannot read file!\n");
free(elevation);
fclose(fp_dem);
return 1;
}
}
else {
printf("Error: cannot get file size!\n");
return 1;
}
``
原文地址: https://www.cveoy.top/t/topic/fsuu 著作权归作者所有。请勿转载和采集!