#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    England, UK
    Posts
    41
    Rep Power
    13

    Split Function for C on Redhat


    Hi

    I am currently working on a program written in C by myself on Redhat Linux 8 where it reads in a file line by line and i need to split up information on that line.

    Example Line:
    ExampleName=ExampleText\n

    What i've been trying to do is split the text at the equals sign (=) so i have two variables where one contains the text "ExampleName" and the other contains "ExampleText".

    Plus i need to remove the text "\n".

    I've already searched through Google and this forum for both but cant seem to find anything, maybe i'm looking for the wrong thing. If anyone can point me in the direction of any tutorials or information i would be very greatful.

    Thanks, Steven.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    As far as I know, C does not have a split function. However, you could use strtok to tokenize your string.

    The first call would be something like:
    strtok(input_string,"=\n");

    strtok returns a pointer to the first token, minus the delimiter ('=' or '\n'). Then call strtok again with a NULL argument:
    strtok(NULL,"=\n");

    strtok will then return a pointer to the second token, or NULL if none was found. BTW, the delimiter string can be different in the second call, but the NULL is needed to continue working on the same input string.

    Hope that helps. I know I sure got spoiled by Perl's split function.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    England, UK
    Posts
    41
    Rep Power
    13
    Hi

    I think i've understood your explanation of strtok but the problem i've got now is when i compile. I believe i have my variables assigned incorrectly. I am trying to add your code to some i seen on the web about opening a file.

    Code:
    while(fgets(file_line, 255, temp_file)!=NULL) {
    	config_name = strtok(file_line,"=\n");
    	config_value = strtok(NULL,"=\n");
    }
    and a bit higher up i have the variable for the file content defined as (this was what the code i found was using):

    Code:
    char file_line[255]= {0,};
    With this as i am new to C i am unsure what i could change it to to fix my problem.

    When i compile with gcc i get the following error message:

    Code:
    test.c:38: incompatible types in assignment
    test.c:39: incompatible types in assignment
    I presume this is to do with file_line as line 38 and 39 are the lines which contain strtok().

    Thanks for your help so far.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,594
    Rep Power
    4207
    >> I presume this is to do with file_line as line 38 and 39 are the lines which contain strtok().
    Why don't you show us how you've declared config_name and config_value. Looking at the warnings, I'm guessing you've got config_name and config_value declared as char arrays.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Being a creature of habit, I tend to think in terms of how I normally use a function.

    As I recall, strtok() uses the char array you pass it, substituting the delimiter with '\0', AKA NULL. Or it may copy to a static buffer first -- I forget.

    Normally, I declare a char* and assign the return value of strtok to it. If not NULL, then I strcpy the string to a variable:

    Code:
    char *cp;
    char field1[80];
    
    if ((cp = strtok(file_line,"=\n")) != NULL)
       strcpy(field1,cp);
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    England, UK
    Posts
    41
    Rep Power
    13
    Hi

    I've got config_name and value like this "char config_name[255]".

    I'm going to have another look now and see if i can get it working but as i said im new to all this so its taking a while but im willing to learn.

    Thanks.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    18
    config_name and config_value need to be declared as "char *".

    Using your example as the contents of file_line:

    ExampleName=ExampleText\n

    The first call to strtok will return a pointer to the beginning of file_line and replace the '=' with a null byte.

    The second call will return a pointer to file_line[12] and replace the '\n' with a null byte. Calling it in a loop will result in config_name and config_value pointing to the appropriate values from the last line placed in file_line. To get the results you want would require that you do something similar to what dwise1_aol suggested.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    England, UK
    Posts
    41
    Rep Power
    13
    Hi

    I've tried both the example code given and changing to "char *config_name;" but i keep getting the following error.

    Code:
    warning: assignment makes pointer from integer without a cast
    I'm going to try and work it out and see if i can get it going.

    Thanks.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    18
    Could you be more specific about which line of code is generating the error?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    England, UK
    Posts
    41
    Rep Power
    13
    Hi

    The same error is reported for both the following lines:

    Code:
    config_name = strtok(file_line,"=\n");
    config_value = strtok(NULL,"=\n");
    and i have the following variables defined in the page

    Code:
    char file_line[255]= {0,};
    char *config_name;
    char *config_value;
    Thanks.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    18
    It apparently thinks that strtok is returning an int which makes me think that maybe you are missing the definition of strtok. Are you including string.h?
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    England, UK
    Posts
    41
    Rep Power
    13
    Hi

    I am not using string.h and i am new to all this so i may have got it wrong. All i want to do is find whats on both sides of the quals sign in a line taken from a file.

    Thanks.
  24. #13
  25. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    I am not using string.h and i am new to all this so i may have got it wrong. All i want to do is find whats on both sides of the quals sign in a line taken from a file.
    Bingo!

    One of the "features" of C is that if a function prototype does not exist, then it will assume that function will return an int. You should have gotten a warning about it, though.

    In my first C class, I made the same kind of mistake. In our first assignment, we were to pass a value to the square-root function, sqrt(), and then print out the value and its square root. I kept getting the weirdest value and just could not figure it out. Same problem: I forgot to include the math.h header, so it assumed sqrt took and returned integers.

    If you include string.h, then it should work a lot better.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    England, UK
    Posts
    41
    Rep Power
    13
    Thank you so much, i added the line "#include <string.h>" and its now working great.

    Everything great now and i can carry on learning C, thanks for all your help.

IMN logo majestic logo threadwatch logo seochat tools logo