please advise....

This is a discussion on please advise.... within the C Programming forums, part of the General Programming Boards category; Hello, Please advise on the best way to extract values of a string so that I can use them to ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    21

    please advise....

    Hello,

    Please advise on the best way to extract values of a string so that I can use them to insert into a db - I am thinking of doing it the following way (untested):

    String is as follows (but will always contain a different amout of detail recs:
    Code:
    ~STATUS*1~RECS*4~ACCESS*1256~NAME*Joe~SNAME*Bloggs~AGE*30~NAME*Joe~SNAME*Bloggs~AGE*30~
    NAME*Joe~SNAME*Bloggs~AGE*30~
    NAME*Joe~SNAME*Bloggs~AGE*30~
    
    ---------------------------------------
    l_param[] = "";
    v_name[10] =""
    v_sname[10] ="";
    v_age[3]  = "";
    
    char *l_cpy_string;
    char *start;
    char *end;
    
    l_cpy_string = strstr(start, "NAME*");
    l_cpy_string++;
    
    while (start!=NULL)
    {
      strcpy(l_param, l_cpy_string);
    
      /* Field 1*/
      start = strstr(l_param, "*");
      start++;
      end = strstr(start, "~")
      *end = '\0';
      
      strcpy(v_name,start); 
    
      /* Field 2*/
      strcpy(l_param, l_cpy_string);
      start = strstr(l_param, "SNAME");
      start++;
      start = strstr(start, "*");
      start++;
      end = strstr(start, "~")
      *end = '\0';
    
      strcpy(v_sname,start); 
    
      /* Field 3*/
      strcpy(l_param, l_cpy_string);
      start = strstr(l_param, "AGE");
      start++;
      start = strstr(start, "*");
      start++;
      end = strstr(start, "~")
      *end = '\0';
    
      strcpy(v_age,start); 
    
      /* Perform insert into DB here using v_* variables */
    
    
      /* Set ptr to next occurence of original string */ 
      l_cpy_string = strstr(start, "NAME*");
      l_cpy_string++;
    --------------------------------

    Cheers,

    Manny

    PS The string will probably have about another 5 fields that will need to be extracted - but I have not included them here as I'll just be copying and pasting the 'Field' section of the code.
    Last edited by Salem; 06-07-2006 at 01:58 AM. Reason: tag

  2. #2
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    why not have each section put into it's own string, in an array, then you only have to work through the array to insert into the database.

    one thing to concider, the name fields may be to small at 10 characters, often a database name field is set to 25 characters, since some names are close to that in length.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,820
    Since you seem to have so many fields to extract, perhaps write a function which
    - extracts the next field from a string
    - returns the remainder of the string
    Then you can call this function in a loop until there is no more string left.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Career Advise
    By ggraz in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-23-2008, 04:57 AM
  2. To Program or Not to Program. Career Advise.
    By SoLost in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-05-2008, 03:58 PM
  3. Need advise on adjency list
    By BoneXXX in forum C Programming
    Replies: 4
    Last Post: 05-23-2007, 12:45 AM
  4. Need some advise
    By ILoveVectors in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2005, 10:24 AM
  5. New student asking for advise
    By Tier in forum C Programming
    Replies: 5
    Last Post: 09-17-2003, 11:23 AM

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