将文件转换为数组保存在内存中

#include <stdio.h>
#include <stdlib.h>


int main() 
{
    FILE *image = fopen("big_face.yuv", "r");
    if(NULL == image)
    {
        printf("open file failed!");
        return 0;
    }
    fseek(image, 0, SEEK_END);
    int nFileLen = ftell(image);
    rewind(image);
    unsigned char* image_dat = (unsigned char*)malloc(nFileLen+1);
    if(NULL == image_dat)
    {
        printf("malloc failed!");
        fclose(image);
        return 0;
    }

    fread(image_dat, sizeof(unsigned char), nFileLen, image);
    image_dat[nFileLen] = '\0';

    fclose(image);

    FILE *f2 = fopen("big_face.c", "w");
    fprintf(f2, "unsigned char big_face[] = {\n");
    
    for (int i = 0; i < nFileLen + 1; i++)
    {
        fprintf(f2, "0x%x", image_dat[i]);
    }
    
    fprintf(f2, "\n};\n");
    fclose(f2);

    return 0;
}
上一篇:微信Dat文件解码


下一篇:实验七