我正在尝试读取以下格式的二进制文件:
图像数量[4字节整数]
width [4-byte int]
高度[4字节int]
灰度数据[宽度*高度字节]
(更多相同类型的元素)
这是第一个被调用的函数:
int process_file(const char *filename) {
FILE *input_file = fopen(filename, "r");
//Get number of images
unsigned int number_of_images = read_int_from_file(input_file);
//Allocate memory for all images
struct image *images = malloc(sizeof(struct image) * number_of_images);
read_images(images, number_of_images, input_file)
}
以下是任何想知道的image
结构:
struct image {
unsigned int width;
unsigned int height;
unsigned char *data;
};
这就是read_int_from_file
的作用:
static unsigned int read_int_from_file(FILE *file) {
unsigned char chars[4];
if (fread(&chars, sizeof(char), 4, file) != 4) {
fprintf(stderr, "Couldn't read enough bytes!\n");
return 0;
}
//Calculations follow that return right numbers
return out;
}
剩下的就是:
static int read_images(struct image *images, unsigned int number_of_images, FILE * file) {
struct image *current_image = images;
int i;
for (i = 0; i < number_of_images; i++) {
read_image(current_image++, file)
}
return EXIT_SUCCESS;
}
static int read_image(struct image *image, FILE *file) {
static long int expected_position = 4;
if (ftell(file) != expected_position) {
fprintf(stderr, "Reading @ %lu when should be @ %lu!",
ftell(file), expected_position);
exit(EXIT_FAILURE);
}
unsigned int width = read_int_from_file(file);
unsigned int height = read_int_from_file(file);
unsigned int size = width * height;
unsigned char *data = malloc(sizeof(char) * size);
if (data) {
if (fread(data, sizeof(char), size, file) != size) {
exit(EXIT_FAILURE);
}
image->width = width;
image->height = height;
image->data = data;
expected_position += 2 * 4 + width * height;
return EXIT_SUCCESS;
} else {
exit(EXIT_FAILURE);
}
}
问题是文件指针有时会在它不应该这样做时继续前进,即我正在点击ftell(file) != expected_position
。经过大量的成功阅读后,我得到了它,但在结束前也有一些好时光。
有没有人任何想法为什么会这样?我的意思是,即使这些数字是错误的,也不应该发生,是吗?谢谢!
答案 0 :(得分:4)
您需要打开二进制文件:
// v
FILE *input_file = fopen(filename, "rb");
答案 1 :(得分:4)
MS-DOS行结束序列是回车符,新行(CR NL
,0x0D, 0x0A
),而Unix只使用新行(NL
或0x0A
)。
更改行
FILE *input_file = fopen(filename, "r");
到
FILE *input_file = fopen(filename, "rb");
否则,fread()
函数用于将CR NL
翻译为NL
,在POSIX标准“IEEE Std 1003.1-1988”之前的Unix系统上。
在MS-DOS,Windows和衍生产品上,它将NL
翻译为CR NL
。
这些翻译使您对文件位置的看法与ftell()
的计算不同。