How to parse the data and retrieve the data.

This is a discussion on How to parse the data and retrieve the data. within the C Programming forums, part of the General Programming Boards category; Hi, I am developing a parser for reading a file and retrieving the data of he file. I want to ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    How to parse the data and retrieve the data.

    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.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
    char seps[] = ", // { }";
    You don't need to backslash slashes, only backslashes.
    Code:
    char * tempbuf = new char[100];
    That's C++. C uses malloc().
    Code:
    char s[80];
    while(fgets(s,79,fp)!= NULL)
    You can use 80 there, or better yet, sizeof(s) or sizeof s.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    new is a C++ keyword. Are you posting in the wrong forum or mixing C and C++ intentionally for some sick and demented reason?
    If you understand what you're doing, you're not learning anything.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    So is this C or C++?
    The 'new' say C++, but most of the rest says C

    > strcmp(tempbuf,"map")>0
    What are you trying to achieve with > 0

    > lastTok = strtok(tempbuf," ");
    You have a memory leak here - you don't need to allocate lastTok at all.
    It simply points to somewhere in tembuf

    In fact, all you really need is
    char * tempbuf[100];
    char * lastTok;

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    Sorry for the errors in the program.Infact I am using both C/C++.Modified code is as below.

    Code:
    [headers]
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    [/headers]
    
    [main]
    int main (int argc, char **argv)
    {
    	FILE *fp;
    	char *read;
    	char s[255];
    	char * tempbuf = new char[100];
    	char * lastTok = new char[100];
    	char seps[] = "{ # , // } ;";
    	int color[255];
    
    	fp = fopen("file1.txt", "r"); //Open the file
    
    	while (fp == NULL)
    	{
    		printf("Cannot open file");
    		return 0;
    	}
    
    	while(fgets(s,200,fp)!= NULL)  //Read the file until EOF 
    	{  
    		strcpy(tempbuf, s);
    	        int enter = 1;
    		
    		if((strchr(tempbuf , '{')!=NULL) && (enter == 1)) 
    			printf("Do Not Read\n");
    
    		if(strchr(tempbuf, '}')!=NULL)
    		{
    			exit(0);
    		}
    		else if (strlen(tempbuf) > 32) 
    		{
    			lastTok = strtok(tempbuf, seps); //Start deleting the symbols
    			while(lastTok != NULL)
    			{
    				printf("%s\n",lastTok);
    				lastTok = strtok(NULL, seps);
    			}
    		}
    	}
    	return 0;
    
    }
    [/main]

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You're still using new, with no deletes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    Be careful. C and C++ are two different languages with different specifications. Some of those specifications contradict specifications in the other language. For instance, the const keyword means something entirely different in both languages. Mixing the two isn't considered a wise move. It's okay to use stuff like printf() in C++, but you should be aware of the differences in the languages.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
    char s[255];
    while(fgets(s,200,fp)!= NULL)
    Your numbers still don't match. (That's not an error, but you're wasting memory.)

    tempbuf isn't large enough to hold all of what might be in s.

    Change tempbuf to the same as s, or
    Code:
    char tempbuf[255];
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    Like they said, don't mix languages, it just makes problems down the line, and that's if you get the source code working.
    To take the string, just take the whole line of input, instead of 255, which might not be long enough, set it to 1024 and then use a sizeof operation to only take what you need.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
    	while(fgets(s,200,fp)!= NULL)  //Read the file until EOF 
    	{  
    		strcpy(tempbuf, s);
    	        int enter = 1;
    
    		if((strchr(tempbuf , '{')!=NULL) && (enter == 1)) 
    			printf("Do Not Read\n");
    Variabes declared in the middle of a block is C++/C99. And enter is always 1.

    Code:
     while (fp == NULL)
    	{
    		printf("Cannot open file");
    		return 0;
    	}
    That code will work, but I think it should be an if.

    Oh yes, and you should fclose() your file.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    Well, I tried - but no body listened....

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    Thanks

    Thanks for all. Infact, there are so many errors in that. I'll take care not to make mistakes like that.

    Kane.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. using class to retrieve and store data
    By FoodDude in forum C++ Programming
    Replies: 9
    Last Post: 08-19-2005, 07:50 AM
  3. Replies: 1
    Last Post: 07-08-2005, 01:26 PM
  4. Retrieve specific data from text for edit
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 01-22-2002, 08:02 AM
  5. Replies: 1
    Last Post: 09-30-2001, 07:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21