Thread: Split line using delimiter

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    41

    Split line using delimiter

    Hello All,

    I have been struggling with this problem for some time now. I've browsed the formum history but didn't find a match for what I'm trying to do.

    I have a big text file in a linux env and I want to read line by line (this is not a problem) and split the readed line into fields. In perl or php it would be easy to do this but as I am a "newbie" C programmer, I'm having some problems.

    So for example the line is:

    2004/12/03 12:01:59;information1;information2;information3

    I want to get this line and put this info to vars like:
    date , var1 , var2 , var3
    so I can use this info latter in the program.

    Can anyone help me with this ?

    Thanks alot,
    Gus

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Look into the strtok() function.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    sscanf() might be a better solution if you need to convert to different data types instead of leaving it in character format.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    41
    I've tryied but with no success.... I realy would need an example. Any help ?

    Thanks,

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Also consider sscanf. [edit]So says the pokey writer sounding like an echo.[/edit]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       const char line[] = "2004/12/03 12:01:59;information1;information2;information3";
       const char *ptr = line;
       char field [ 32 ];
       int n;
       while ( sscanf(ptr, "%31[^;]%*c%n", field, &n) == 1 )
       {
          ptr += n;
          printf("field = \"%s\"\n", field);
       }
       return 0;
    }
    
    /* my output
    field = "2004/12/03 12:01:59"
    field = "information1"
    field = "information2"
    field = "information3"
    */
    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.*

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    41
    Thank you all for the help !! Now it works and I can do what I was trying... a special thanks to Dave_Sinkula !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  2. How to split up CAN data line
    By rkooij in forum C Programming
    Replies: 6
    Last Post: 03-15-2006, 06:24 AM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM