Thread: Extracting strings from a buffer

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    4

    Extracting strings from a buffer

    Hi.

    I am trying to extract a series of strings that are put together in a buffer like this:

    David_Smith__monkey___555-1435__555-1323__test.com_

    one _ equals one VTAB.

    The strings should be inserted in a struct, the struct looks like this:
    struct Contact {
    char fname[50];
    char sname[50];
    char nmame[50];
    char home[50];
    char next[50];
    char temp[50];
    };

    So, Devid should go in fname, Smith in sname, nname should be skipped since there is nothing between the two TABS, etc.

    I am stuck so any help would be very appreciated.
    /MoonSire

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This looks like a job for sscanf. Then again, you said that empty fields are allowed, so it may be more convenient to manually parse or take advantage of some of the string searching functions in string.h. What have you tried so far?
    My best code is written with the delete key.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    How about this:
    Code:
    ... CENSORED
     
        char  *sep = strtok(buff, DELIM);
    
        if (sep != NULL)
        {
          strcpy(ct.fname, sep);
          sep = strtok(NULL, DELIM);
         
    ... CENSORED
    Last edited by Micko; 07-28-2006 at 06:56 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by MoonSire
    David_Smith__monkey___555-1435__555-1323__test.com_

    one _ equals one VTAB.

    [...]

    So, Devid should go in fname, Smith in sname, nname should be skipped since there is nothing between the two TABS, etc.
    Quote Originally Posted by Micko
    How about this:
    Code:
        char  *sep = strtok(buff, DELIM);
        if (sep != NULL)
        {
          strcpy(ct.fname, sep);
          sep = strtok(NULL, DELIM);
    strtok doesn't handle multiple consecutive delimiters in the desired fashion.

    Perhaps one of these instead:
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'll also need a function to subsitute the occurrence of 'a' with the letter 'e'.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    On the link for using strcspn Part 1, I didnt think the following line was legal c code

    Code:
    printf("line %d:\n", ++k);
    Maybe I mis-understood the concept of a sequence point from the c faq.
    http://c-faq.com/expr/seqpoints.html

    and
    http://c-faq.com/expr/evalorder2.html

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    What's wrong with it? k is only being evaluated once, so there is no ambiguity about the expression. If k was used twice in the expression, then you might have trouble. In this case there is no trouble.

  8. #8
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by Dave_Sinkula
    strtok doesn't handle multiple consecutive delimiters in the desired fashion.
    You're right, I didn't pay attention
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  9. #9
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    Thanks guys!

    I completely overlooked›sscanf so my solution was extracting each char from the buffer and inserting it into the struct with an array of pointers to each variable in the struct

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    I tried using this one:

    #include <stdio.h>
    ..
    int main(void)
    {
    const char line[] = "2004/12/03 12:01:59;info1;info2;info3";
    const char *ptr = line;
    char field [ 32 ];
    int n;
    while ( sscanf(ptr, "%31[^;]%n", field, &n) == 1 )
    {
    printf("field = \"%s\"\n", field);
    ptr += n; /* advance the pointer by the number of characters read */
    if ( *ptr != ';' )
    {
    break; /* didn't find an expected delimiter, done? */
    }
    ++ptr; /* skip the delimiter */
    }
    return 0;
    }

    But modified it with a "[^\t]" instead of the "[^;] and changed the line to "David\tapa\tkorv\tfillefjong\t104510\t\tnisse ";
    But the only output I get then is
    field = "David"

    I looked in man page for sscanf and the way I understand it nothing else should need to be changed...Am I wrong?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Aside from not using code tags...

    Shouldn't you also change the break condition?


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    well well, i'm off to bed

    thanks quzah!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 05:41 AM
  3. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  4. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  5. DirectSound - multiple sounds
    By Magos in forum Game Programming
    Replies: 9
    Last Post: 03-03-2004, 04:33 PM