Split line

This is a discussion on Split line within the C Programming forums, part of the General Programming Boards category; Hello All, I'm trying to split a line like this: 15 /usr/bin/php /html/echo.php 8 I split paramaters using space char ...

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

    Split line

    Hello All,


    I'm trying to split a line like this:

    15 /usr/bin/php /html/echo.php 8

    I split paramaters using space char as the delimiter. I need to put the first par (15) and a variable and all the rest to another variable. I've tryed to do this using strtok, I can read all the params but didnt had success to put all the other pars to one unique variable.

    Here is a small part of my code:

    Code:
                                    while(c<1000) {
                                            if(c==0) {
                                                    strcpy(comando,token);
                                            } else { //if(c==1) {
                                                    strcpy(valor,token);
                                                    x = strlen(valor)-1;
                                                    if(valor[x] == CHAR_RETURN) {
                                                            puts("fim!");
                                                            break;
                                                    }
                                            }
    
                                            printf("valor: [%s]\n",valor);
                                            printf("Xtoken: [%s]\n",token);
    
                                            token = strtok( NULL, CHAR_DELIM );
                                            printf("token2: [%s]\n",token);
                                            printf("before->buffer: [%s]\n",token);
                                            sprintf(buffer,"%s %s",buffer,token);
                                            printf("after->buffer: [%s]\n",token);
                                            printf("===========> c: [%d]\n",c);
                                            c++;
                                    }
    Can anybody point me a better way to do this ?

    Thanks in advance,

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > strcpy(valor,token);
    It sounds like you want to concatenate the parameters, so this would be:
    strcat(valor,token);

    Then before the while() loop, you would need:
    valor[0] = '\0';

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And you probably want them separated by a space so:
    Code:
    strcat(valor,token);
    strcat(valor," ");

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    41
    Well, I think Ive found a better way of doing what I need. Ive done this:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    main() {
            //char line[]="Hello World !!";
            //char line[]="Hello My Big World !! 1 2 3 4 5 6 7 8 9 0";
            char line[]="Hello My Big World";
            //char line[]="15 /usr/bin/php /html/echo.php 8";
            //char line[BUFSIZ];
            //strcpy(line,"Hello My Big World");
    
            func(line);
    }
    
    func(char *ptr) {
    
            int y=0,c=0,n,len=0,x=0;
            char periodicidade[BUFSIZ];
            char path[BUFSIZ];
            char field[BUFSIZ];
            char *p_texto = ptr;
    
               while ( sscanf(ptr, "%50[^ ]%*c%n", field, &n) == 1 )
               {
                    len = strlen(ptr);
                    ptr += n;
                    switch(c) {
                            case 0:
                                    strcpy(periodicidade,field);
                                    printf("field[%d]: [%s]\n",c,field);
                                    break;
                            /*
                            case 1: 
                                    printf("field[%d]: [%s]\n",c,field);
                                    break;
                            case 2: 
                                    printf("field[%d]: [%s]\n",c,field);
                                    break;
                            */
                            default:
                                    strcat(path,field);
                                    strcat(path," ");
                                    printf("default->field[%d]: [%s]\n",c,field);
                                    break;
                    } //switch
                    c++;
                 } //while
    
            puts("");
            puts("=====================================================");
            printf("periodicidade: [%s]\n",periodicidade);
            printf("path: [%s]\n",path);
    
    }
    The result was:
    field[0]: [Hello]
    default->field[1]: [My]
    default->field[2]: [Big]
    default->field[3]: [World]
    default->field[4]: [d]
    default->field[5]: [@]

    ================================================== ===
    periodicidade: [Hello]
    path: [My Big World d @ ]


    As we can see at the end of the string there are some trashed chars... I think that is info on memory...
    Can anyone explain what is this and how to take it off ?

    Thanks again,

  5. #5
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    Quote Originally Posted by groorj
    Well, I think Ive found a better way of doing what I need. Ive done this:

    ....
    sscanf(ptr, "%50[^ ]%*c%n", field, &n)
    .

    As we can see at the end of the string there are some trashed chars... I think that is info on memory...
    Can anyone explain what is this and how to take it off ?

    Thanks again,
    It's trash from your memory and it usually tells, there's something went wrong unless you haven't purposly done so.
    You force sscanf read after end of your initialized character array.
    and here's scanf format for you which propably fits in this case. "%s%n"
    -- Add Your Signature Here --

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    41
    Quote Originally Posted by tjohnsson
    ... here's scanf format for you which propably fits in this case. "%s%n"
    Well it didnt realy work !

    Code:
    while ( sscanf(ptr, "%50[^ ]%*s%n", field, &n) == 1 )
    It produces the following:
    field[0]: [Hello]

    ================================================== ===
    periodicidade: [Hello]
    path: []

    Dont know what to do....

    Thanks,

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by groorj
    Well, I think Ive found a better way of doing what I need. Ive done this:
    Well it's not really a better way if it doesn't work, now is it?

    Quote Originally Posted by groorj
    Code:
            char line[]="Hello My Big World";
    
            ...snip...
    The result was:
    field[0]: [Hello]
    default->field[1]: [My]
    default->field[2]: [Big]
    default->field[3]: [World]
    default->field[4]: [d]
    default->field[5]: [@]
    How could you possibly expect that to work? You have three fields, but you try and display five. What would you expect to be in the last two?

    Why don't you just do it the easy way:
    Code:
    while we still have a string
        skip all the spaces
        copy everything until a space into some place else
        add a null onto this 'some place else' so it's a valid string
    That's the basic process, and it's quite simple. I'll leave the implementation to you. Or ignore it as you see fit.

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

  8. #8
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    Quote Originally Posted by groorj
    Well it didnt realy work !

    Code:
    while ( sscanf(ptr, "%50[^ ]%*s%n", field, &n) == 1 )
    It produces the following:
    field[0]: [Hello]

    ================================================== ===
    periodicidade: [Hello]
    path: []

    Dont know what to do....

    Thanks,
    sscanf(ptr, "%s%n", field, &n)

    Try again...
    -- Add Your Signature Here --

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    41
    Hey, thanks alot tjohnsson.

    It worked !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 04:36 AM
  3. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  4. How to split up CAN data line
    By rkooij in forum C Programming
    Replies: 6
    Last Post: 03-15-2006, 06:24 AM

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