Thread: Dumping fields into variables

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    76

    Dumping fields into variables

    I have a file that we are supposed to use as input. It looks like this:
    Code:
    name:curriculum:gradyear:ssn:dob
    I'm going to read it in a buffer, but trying to find the logic to seperate the fields and then place in a variable. I was thinking something like:
    Code:
    if(ispunct(buf[x]) != ':') 
         strcpy(s->name, buf);
    Any suggestions is greatly appreciated!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you could use
    - strtok(), but watch out for it modifying the source string. You need to copy the results elsewhere ideally.
    - sscanf() with a scan-set conversion. For example
    Code:
    sscanf( buff, "%[^:]", name );
    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.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Hmmm, strtok(). Unaware of that function. Anyone might have an example on how its used?

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    "You can teach a fish to ride a bicycle, but you can't make him think"
    Search: Key Word(s): strtok Showing results 1 to 25 of 323
    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.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    I'm aware of keyword searches. I just figured someone(thanks sand_man!!) had a quick link.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I just figured someone(thanks sand_man!!) had a quick link.
    Your message posted at Yesterday, 11:54 PM
    Next reply posted at Today, 01:55 AM
    Wait 2 hours for a reply, or get answers in a few seconds with a search - go figure.
    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.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Or troll around 323 threads for 2 hours when someone has exactly what I needed- go figure . Thanks for your help eveyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  2. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  3. Lists: adding fields (not nodes) & more
    By Mariano L Gappa in forum C++ Programming
    Replies: 15
    Last Post: 11-09-2005, 07:26 PM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM