打包资源文件到程序

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

void FileToArray(const char *srcfile, const char *dstfile)
{
    FILE *file = fopen(srcfile, "rb");
    if (file)
    {
        FILE *save = fopen(dstfile, "w");
        fseek(file, 0, SEEK_END);
        unsigned int fileLen = ftell(file);

        fprintf(save, "static unsigned int gAssetsLen=%ld;\n", fileLen);
        fprintf(save, "static unsigned int gAssetsData[]=\n");
        fprintf(save, "{\n");

        fseek(file, 0, SEEK_SET);
        unsigned int arrLen = (fileLen + 3) / 4;
        unsigned int *dataArr = new unsigned int[arrLen];
        fread(dataArr, 1, fileLen, file);
        for (int i = 0; i < arrLen; ++i)
        {
            fprintf(save, "0x%08x", dataArr[i]);
            //fprintf(save, "0x%x", dataArr[i]);
            if (i != arrLen - 1){ fprintf(save, ","); }
            if (9 == i % 10){ fprintf(save, "\n"); }
        }
        fprintf(save, "};\n");
        fclose(save);
    }
    fclose(file);
}
void main()
{
    FileToArray("assets.zip","assets.h");
}

 

posted @ 2015-07-19 14:32  liusijian  阅读(249)  评论(0编辑  收藏  举报