Hi,
I am developing a parser for reading a file and retrieving the data of he file. I want to read individual part of data and store it in the local variable.I am able to read & store the first string, but not able to read & retrieve the remaining string in the file.Can any one help me in solving this problem.I am attaching the code and a sample file.
Code:
[headers]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
[/headers]
[main]
int main(int argc, char ** argv)
{
FILE *fp;
char s[80];
char * tempbuf = new char[100];
char * lastTok = new char[100];
char seps[] = ", // { }";
unsigned short x[50];
unsigned short y[50];
fp = fopen("file1.txt","r");
if (fp == NULL)
{
puts ("Cannot open file");
return 0;
}
while(fgets(s,79,fp)!= NULL)
{
int buffer=1;
strcpy(tempbuf, s);
if((buffer==1) && (strncmp(tempbuf,"const u",7)==0))
{
buffer = 0;
}
if (strlen(tempbuf) > 32)
{
buffer = 1;
if(strchr(tempbuf, '}')!=NULL)
{
exit(0);
}
else if(buffer == 1)
{
lastTok = strtok(tempbuf," ");
while(lastTok != NULL)
{
printf("%s\n",lastTok);
for(int i=0; i<5; i++)
{
x[i] = atoi(lastTok);
printf("%d\n",x[i]);
}
lastTok = strtok (NULL, seps);
}
}
}
if((strcmp(tempbuf,"map")>0)|| (strlen(tempbuf) > 32))
{
buffer = 1;
{
if(strrchr(tempbuf, '}')!=NULL)
{
exit(0);
}
if(buffer == 1)
{
lastTok = strtok(tempbuf," ");
while(lastTok != NULL)
{
for(int i=0; i<1; i++)
{
y[i] = atoi(lastTok);
}
lastTok = strtok (NULL, seps);
}
}
}
}
}
return 0;
}
[/main]
[File]
[string1]
const u8 pal[5 * 5] =
{
0x7c1f,0x8bef,0x8acf,0x0000,0xfec1,
0x7c1f,0x8bef,0x8acf,0x0000,0xfec1,
0x7c1f,0x8bef,0x8acf,0x0000,0xfec1,
0x7c1f,0x8bef,0x8acf,0x0000,0xfec1,
0x7c1f,0x8bef,0x8acf,0x0000,0xfec1
}
[/string1]
[string2]
const u16 map[8 * 8]=
{
0x5aed,0x32ac,0x9bdc,0x231c,0x8cde,0xfbce,0x4cde,0 x756b,
0x5aed,0x32ac,0x9bdc,0x231c,0x8cde,0xfbce,0x4cde,0 x756b,
0x5aed,0x32ac,0x9bdc,0x231c,0x8cde,0xfbce,0x4cde,0 x756b,
0x5aed,0x32ac,0x9bdc,0x231c,0x8cde,0xfbce,0x4cde,0 x756b,
0x5aed,0x32ac,0x9bdc,0x231c,0x8cde,0xfbce,0x4cde,0 x756b,
0x5aed,0x32ac,0x9bdc,0x231c,0x8cde,0xfbce,0x4cde,0 x756b,
0x5aed,0x32ac,0x9bdc,0x231c,0x8cde,0xfbce,0x4cde,0 x756b,
0x5aed,0x32ac,0x9bdc,0x231c,0x8cde,0xfbce,0x4cde,0 x756b
}
[/string2]
[/File]
Thanks in advance,
Kane.