On Fri, 12 Dec 2003 22:02:24 -0700, Jeff Rodriguez
<> wrote in comp.lang.c:
> Ben Pfaff wrote:
>
> > It depends on the operating system, shell, and other things. If
> > all you want to do is to break apart the string into words at
> > white space, I suggest you just write code to do it. It's not
> > too hard.
>
> Indeed: (crude code)
> #include <stdio.h>
> #include <string.h>
> #include <unistd.h>
The header above is both non-standard and unneeded in your program.
> #include <stdlib.h>
>
> int main(int argc, char *argv[])
> {
> int i;
> char *string = NULL;
> char *str = NULL;
> int size = 0;
>
> for ( i = 1; i < argc; i++ )
> {
> if ( strlen(argv[i]) > size )
> {
> if ( (string = realloc(string, (size + strlen(argv[i])) * 2)) == NULL )
> {
> perror("Could not allocate memory");
> exit(1);
> }
> }
>
> string = strcat(string, argv[i]);
This is quite likely to crash the first time, since the first block
that realloc returns is uninitialized and there's no telling if it
will contain a terminating '\0' at all, let alone in the first
element.
> string = strcat(string, " ");
> }
>
> printf("%s %s\n", argv[0], string);
>
> str = strtok(string, " \t\n");
> printf("%s\n", str);
> while ( (str = strtok(NULL, " \t\n")) != NULL )
> {
> printf("%s\n", str);
> }
> return 0;
> }
>
> However you run into problems like:
> ./a.out --search="Hello World!"
>
> I would prefer not to have to reinvent the wheel on this since main() already
> implements it! There must be /some/ way that it's a built-in function!
>
> Jeff
No, main() does not already implement it. Either the host operating
system or the implementation's start-up code does. There is no C
standard library function to do so.
If you want source code, try Google.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
ftp://snurse-l.org/pub/acllc-c++/faq