Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

equivalent to cut command in unix

Status
Not open for further replies.

ahsanamin

Programmer
Jun 14, 2002
3
US
Hi,
I am very familiar with unix commands but not as familiar with C. Can anyone provide me a function in C which works similar to cut fuction in unix. The cut command is very useful for parsing strings. Basically my problem is that I have a string of characters delimited by colons :)) and I would like my program to read each line and read the data in each field into a variable.
for example if the string is
1234:232:222
I would like to read 1234 into variable a, 232 into b and 222 into c.
Thanks
Ahsan
 
Ahsan:

Unfortunately, the standard "C" libary doesn't contain a cut command, but the string tokenizer function, strtok should do the trick of splitting:

1234:232:222

into variables. What's unfortunate, is if you have a null field:

1234:232:::::222

That's why I have my own version of strtok called xstrtok. Here's an example using the pipe symbol, |, as a delimiter:

Regards,

Ed

#include <stdio.h>

/* example tokenizeing a string */

int main(argc,argv)
int argc;
char **argv;
{
char *xstrtok();
char *strtok();
char *string = &quot;a|string|of||tokens&quot;;
char *field1, *field2, *field3, *field4, *field5;

field1=xstrtok(string,&quot;|&quot;);
field2=xstrtok(NULL, &quot;|&quot;);
field3=xstrtok(NULL, &quot;|&quot;);
field4=xstrtok(NULL, &quot;|&quot;);
field5=xstrtok(NULL, &quot;|&quot;);

printf(&quot;field 1 is %s\n&quot;, field1);
printf(&quot;field 2 is %s\n&quot;, field2);
printf(&quot;field 3 is %s\n&quot;, field3);
printf(&quot;field 4 is %s\n&quot;, field4); /* should be null */
printf(&quot;field 5 is %s\n&quot;, field5);

exit(0);
}

/*
* strtok version that handles null fields
*/
char *xstrtok(line, delims)
char *line, *delims;
{
static char *saveline = NULL;
char *p;
int n;

if(line != NULL)
saveline = line;

/*
*see if we have reached the end of the line
*/
if(saveline == NULL || *saveline == '\0')
return(NULL);
/*
*return the number of characters that aren't delims
*/
n = strcspn(saveline, delims);
p = saveline; /*save start of this token*/

saveline += n; /*bump past the delim*/

if(*saveline != '\0') /*trash the delim if necessary*/
*saveline++ = '\0';

return(p);
}
 
Hi,

Just a little error in the xstrok example. When you define a char * like this...

char *string = &quot;a|string|of||tokens&quot;;

You can't do this in your xstrok() function...

*saveline++ = '\0';

Initializing a char * makes this an an &quot;unnamed, static array of characters, which may be stored in read-only memory, which is why you can't safely modify it.&quot; (Taken from comp.lang.c Frequently Asked Questions -
So you should initialize the character string like this

char string[] = &quot;a|string|of||tokens&quot;;

This will allow you to modify the characters stored in there.

HTH -Tyler
 
Thanks a lot guys for all your help. It will help me a lot with my program.
Thanks
Ahsan
 
hi Olded,
i checked ur function as u said for my question of parsing and it works fine on an AIX box. but when i am testing on a linux box it throws me a memseg error in xstrtok function at the following place

if(*saveline != '\0') /*trash the delim if necessary*/
*saveline++ = '\0';

do we need to add any check..
 
I thought i would rather put the above this way..the code works fine with XLC compiler and it throws the above Segmentation error with GCC compiler.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top