根据别人的代码改了一个分析文件的代码,有点错误高手帮忙看看那不对了。。。。。。
文件里面的内容格式就是这种的。。<FilmData>
<FilmCount>4</FilmCount>
<Film>
<Title><![CDATA金庸01]]></Title>
<Director><![CDATA[]]></Director>
<Actor><![CDATA[]]></Actor>
</Film>
<Film>
<Title><![CDATA金庸02]]></Title>
<Director><![CDATA[]]></Director>
<Actor><![CDATA[]]></Actor>
</Film>
<Film>
<Title><![CDATA金庸03]]></Title>
<Director><![CDATA[]]></Director>
<Actor><![CDATA[]]></Actor>
</Film>
<Film>
<Title><![CDATA金庸04]]></Title>
<Director><![CDATA[]]></Director>
<Actor><![CDATA[]]></Actor>
</Film>
<FilmData>
我的代码如下:
大概的思路是先将文件按全部读入到pbuf中,然后掉用sal_get_buf()sal_get_txt()函数来读取
<Film>
<Title><![CDATA金庸]]></Title>
<Director><![CDATA[]]></Director>
<Actor><![CDATA[]]></Actor>
</Film>
这里面的各项数据,现在问题是我的for循环来时在重复的读取第一个<file></file>之间的数据,我已经记录了每一次读取数据的指针然后再读,还是没找到错误在那。。。。
void load_GetHotClipInfos_info()
{
char *pbuf = NULL;
int file_size;
FILE *fp = NULL;
int i=0;
int len=0;
char *ptr;
int cont=0;
char *tmp;
char contbuf[100];
fp = fopen("/Infos.txt", "rb");
if( fp == NULL )
{
TRACE(ERROR, PLAYER_MODE, "fopen [%s] failed !\n", filename);
return ;
}
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
pbuf = malloc(file_size);
file_size = fread(pbuf, 1, file_size, fp);
fclose(fp);
memset(&film, 0, sizeof(film));
ptr = calloc(1, PTR_SIZE);
len = sal_get_buf( "FilmData", pbuf, ptr, PTR_SIZE);
if(len == 0 )
{
printf( "ERROR!\n");
free(ptr);
free(pbuf);
return ;
}
cont=sal_get_int("FilmCount", ptr);
tmp=pbuf;
for(i=0;i<cont;i++)
{
len = sal_get_buf( "Film", tmp, ptr, PTR_SIZE);
strcpy( film[i].Title, sal_get_txt("Title", ptr));
strcpy( film[i].Director, sal_get_txt("Director", ptr));
strcpy( film[i].Actor, sal_get_txt("Actor", ptr));
printf("%s\n" , film[i].Title);
printf("%s\n" , film[i].Director);
printf("%s\n" , film[i].Actor);
tmp=tmp+len;
}
}
char *sal_get_txt(char *pKey, char *txtBuf)
{
char *txt = NULL;
char *txtStar, *txtEnd;
if (strlen(pKey) > 64)
{
return NULL;
}
memset(Header, 0, sizeof(Header));
memset(Tailer, 0, sizeof(Tailer));
memset(outTxt, 0, sizeof(outTxt));
sprintf(Header, "<%s>", pKey);
sprintf(Tailer, "</%s>", pKey);
txtStar = strstr(txtBuf, Header);
txtEnd = strstr(txtBuf, Tailer);
txt = txtStar + strlen(Header);
if ((txtEnd-txt) > 64)
{
TRACE(ERROR, SAL_MODE, "the body is too long\n");
return NULL;
}
memcpy(outTxt, txt, (txtEnd-txt));
return outTxt;
}
int sal_get_int(char *pKey, char *txtBuf)
{
char *pNum;
pNum = sal_get_txt(pKey, txtBuf);
if (pNum != NULL)
{
return strtol(pNum, NULL, 10);
}
return 0;
}
int sal_get_buf(char *pKey, char *txtBuf, char *pDstTxt, int length)
{
char *txt = NULL;
char *txtStar, *txtEnd;
memset(Header, 0, 70);
memset(Tailer, 0, 70);
sprintf(Header, "<%s>", pKey);
sprintf(Tailer, "</%s>", pKey);
txtStar = strstr(txtBuf, Header);
txtEnd = strstr(txtBuf, Tailer);
txt = txtStar + strlen(Header);
if ((txtEnd-txt) > length)
{
TRACE(ERROR, SAL_MODE, "the body is too long (%d) \n", txtEnd-txt);
return 0;
}
memcpy(pDstTxt, txt, (txtEnd-txt));
pDstTxt[txtEnd-txt] = 0;
return (txtEnd+strlen(Tailer));
}
[ 本帖最后由 cz737 于 2011-2-26 19:52 编辑 ]