码迷,mamicode.com
首页 > 其他好文 > 详细

打包资源文件到程序

时间:2015-07-19 14:58:31      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:

#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");
}

 

打包资源文件到程序

标签:

原文地址:http://www.cnblogs.com/liusijian/p/4658610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!