-1

我正在尝试使用 android ndk 中的 C 函数计算大型 bmp 文件的大小。我正在使用以下代码:-

int getSizeOfCapturedImage(char * imagePath) {
    FILE *imageFile = fopen(imagePath, "rb");
    long sizeOfImageFile = 0;

    if (imageFile == NULL) {
        printf("file not found!\n"); //handle error
        return 1;
    } else {
        fseek(imageFile, 0, SEEK_END);
        sizeOfImageFile = ftell(imageFile);
        printf("size of ImageFile is %1dB\n", sizeOfImageFile);
        fclose(imageFile);
    }
    return sizeOfImageFile;
}

但是,当 bmp 文件的大小大于 120 字节时,此功能无法正常工作。该文件以某种方式无法打开,并且文件指针 *imageFile 变为 NULL。

谁能告诉我为什么会这样和/或有另一种出路?

4

1 回答 1

1

查看此 SO post,其中有一种解决方法,不需要您使用 fopen 打开文件。使用的代码是:

#include <sys/stat.h>

ssize_t fsize(const char *filename) {
    struct stat st; 

    if (stat(filename, &st) == 0)
        return st.st_size;

    return -1; 
}

@H2CO3 的好点子:

数据类型:ssize_t 此数据类型用于表示在单个操作中可以读取或写入的块的大小。它类似于 size_t,但必须是有符号类型。

于 2012-12-31T05:41:03.793 回答