| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 529 人关注过本帖
标题:三维变长数组读取接口
只看楼主 加入收藏
新浪
Rank: 3Rank: 3
来 自:水星
等 级:论坛游侠
威 望:1
帖 子:770
专家分:167
注 册:2008-6-10
结帖率:91.67%
收藏
已结贴  问题点数:20 回复次数:2 
三维变长数组读取接口
程序代码:
#include <stdio.h>
#include <stdlib.h>

typedef signed char byte;
typedef unsigned char ubyte;

int readInt(ubyte* input, int skip);
short readShort(ubyte* input, int skip);
byte readByte(ubyte* input, int skip);
ubyte* bgLoadResource(char* res_name);

int main(void)
{

    byte*** arr = NULL;
    ubyte* buffer = NULL;    

    int i, j, k, len3, len2, len, skip;

    len = 0;
    skip = 0;

    buffer = bgLoadResource("blueguy.bin");

    len3 = readShort(buffer, skip);
    skip += 2;

    arr = malloc(sizeof(ubyte**) * len3);     //创建第一维

    for (i = 0; i < len3; i++)
    {
        len2 = readShort(buffer, skip);
        skip += 2;
       
        arr[i] = malloc(sizeof(ubyte*) * len2);   //创建第二维

        for (j = 0; j < len2; j++)
        {
            len = readShort(buffer, skip);
            skip += 2;
       
            arr[i][j] = malloc(sizeof(ubyte) * len);  //创建第三维

            for (k = 0; k < len; k++)
            {
                arr[i][j][k] = readByte(buffer, skip);
                skip += 1;
            }
        }
    }

    return 0;
}

ubyte* bgLoadResource(char* res_name)
{
    FILE* fp = NULL;
    ubyte* data = NULL;
    int size = 0, ret = 0;

    if ((fp = fopen(res_name, "rb"))==NULL)
    {
        printf("File Not Found : %s\n", res_name);
        return NULL;   
    }
   
    fseek(fp, 0L, SEEK_END);      
    size = ftell(fp);

/*  if SEEK_END not supported, use this instead           */
/*  size = 0;
    while (getc(fp) != EOF)
        size++;
*/
    data = (ubyte*)malloc(sizeof(ubyte) * size);

    fseek(fp, 0L, SEEK_SET);
    if ((ret = fread(data, size, 1, fp)) != 1)
    {
        printf("Error reading image data from %s.\n", res_name);
        return NULL;
    }
   
    fclose(fp);

    return data;   
}

int readInt(ubyte* input, int skip)
{
    int c, c1, c2, c3;
    c = input[skip];
    c1 = input[skip+1];
    c2 = input[skip+2];
    c3 = input[skip+3];

    return (c3) |
        (c2 << 8) |
        (c1 << 16) |
        (c << 24);
}

short readShort(ubyte* input, int skip)
{
    short c, c1;
    c = input[skip];
    c1 = input[skip+1];

    return (c1) |
        (c << 8);
}

byte readByte(ubyte* input, int skip)
{
    return input[skip];
}
blueguy.bin 每维先存维数, 再存数据。
另一种方式,就是将维数单独存在一个文件里。

[ 本帖最后由 新浪 于 2010-8-14 13:31 编辑 ]
搜索更多相关主题的帖子: 三维 接口 
2010-08-14 13:03
新浪
Rank: 3Rank: 3
来 自:水星
等 级:论坛游侠
威 望:1
帖 子:770
专家分:167
注 册:2008-6-10
收藏
得分:0 
未经测试, 估计问题不大。 见论坛有朋友对文件读写比较陌生,
于是写了这个,学习一下吧。
return (c1) +
    (c << 8);
写成下面这样最好
return (c1) |
    (c << 8);
体现一下技术含量, 我已经换了。

[ 本帖最后由 新浪 于 2010-8-14 13:24 编辑 ]

天下皆醒,唯我独醉;  天下皆白,唯我独黑
2010-08-14 13:05
jack10141
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:陕西西安
等 级:小飞侠
威 望:6
帖 子:706
专家分:2271
注 册:2010-8-10
收藏
得分:20 
谢谢分享!

Coding就像一盒巧克力,你永远不会知道你会遇到什么BUG
别跟我说你是不能的,这让我愤怒,因为这侮辱了你的智慧
2010-08-14 13:29
快速回复:三维变长数组读取接口
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016078 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved