Thread: simple question about strtok

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    simple question about strtok

    I have the following text file:

    Anna 199.9 2000.2
    John 300.9 400.2
    Linda 200.9 7.00
    Harry 90.9 20.9
    Code:
      char * split;
      printf ("Splitting string \"%s\" in tokens:\n",buffer);
      split = strtok (buffer,"  ");
      while (split != NULL)
      {
        printf ("%s ",pch);
        split = strtok (NULL, " ,");
     }
    I've used strok, and have sucessfully splitted the 3 rows into seperate tokens. The problem i'm having is storing these values temporarly somewhere so i can add them to a link list. I can't assign the splitted value to a new char but then when the loop creates another token this will be overwritten.

  2. #2
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    tokenized:


    Anna
    199.9
    2000.2
    John
    300.9
    400.2
    Linda
    200.9
    7.00
    Harry
    90.9
    20.9

  3. #3
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    Why not just use sscanf() instead of strtok()?

  4. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    I suppose i could use sscanf, but then i have that problem again where i want to store it temporarly but it's overwritten with the latest value.
    Last edited by Axel; 10-16-2005 at 12:09 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > The problem i'm having is storing these values temporarly somewhere so i can add them to a link list.
    Do your linked list nodes contain
    char *name;
    or
    char name[30];

    Sure you can just keep the pointers while you're in the strtok() loop, but you need to have allocated memory and several strcpy() calls to preserve all the information you've extracted in a unique place.
    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
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    >Do your linked list nodes contain
    >char *name;
    >or
    >char name[30];

    just char name;

    >but you need to have allocated memory and several strcpy() calls to >preserve all the information you've extracted in a unique >place.

    hmm i'm assuming by serveral strcpy's you mean that i'm going to have to copy the name (that is tokenized) and total earnings somewhere to several variables?
    Last edited by Axel; 10-16-2005 at 03:04 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes (you need an array of chars)

    Yes (you need to copy each field you extract)
    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
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    > Yes (you need to copy each field you extract)

    Hmm... i guess that would work but it's a bad design. What happens if i use a different txt file with only 2 customers or more than 4?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You said you had a linked list.
    To me, that meant one node on the list per customer, and you grow the list for as many customers you have.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  2. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  3. strtok() question
    By Bajanine in forum C Programming
    Replies: 3
    Last Post: 12-30-2004, 04:58 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM