Newbie string problem

J

jorntk

what is the best ways to break a string like "ls -al | grep abc" in
"ls" as an variable then the following argument is put in an array?
thanks in advance.
 
N

Neil Cerutti

what is the best ways to break a string like "ls -al | grep
abc" in "ls" as an variable then the following argument is put
in an array? thanks in advance.

Can you be more specific? Show an example of what you would like
as a result.
 
T

Thomas Matthews

what is the best ways to break a string like "ls -al | grep abc" in
"ls" as an variable then the following argument is put in an array?
thanks in advance.

strtok()
strcpy()
strncpy()
strstr()
strpbrk()

Take a look in your language manual in the section about strings.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

jorntk

Neil Cerutti said:
Can you be more specific? Show an example of what you would like
as a result.

i want to break the string came from user input to become the input of
execvp command.
 
J

jorntk

Neil Cerutti wrote in message ...
Go on, we're listening...

(What's "the input of execvp command"? How do you want to "break the
string", and where?)

-Arthur

i want to break the string like "ls -al | grep abc into individual
word "ls" "-al" "|" "grep" "abc" and put them in array like
char* arg_list[]=
{
"ls",
"-al",
"|",
"grep",
"abc"
NULL
};
a variable program will be the first element of the array arg_list.
the two variable will be use as the parameter of procedure spawn that
will execute the command entered by user.
int spawn(char* program, char** arg_list)
 
N

Neil Cerutti

Go on, we're listening...

(What's "the input of execvp command"? How do you want to
"break the string", and where?)

-Arthur

i want to break the string like "ls -al | grep abc into individual
word "ls" "-al" "|" "grep" "abc" and put them in array like
char* arg_list[]=
{
"ls",
"-al",
"|",
"grep",
"abc"
NULL
};
a variable program will be the first element of the array
arg_list. the two variable will be use as the parameter of
procedure spawn that will execute the command entered by user.
int spawn(char* program, char** arg_list)[/QUOTE]

Thanks for the specification.

The program you want is not complicated, it just requires good
technique.

You are going to have to manage memory in order to solve this in
the most general way. In other words, if you want a solution that
will work for an arbitrary number of arguments and for arbitrary
length arguments, you need to manage memory. If you want a
solution without the drawbacks of strtok, you'll need to invent
your own simple algorithm to parse the data.

Consequently, a nice general solution, will take me too long to
write. ;-)

A simple solution using strtok and a limited number of args,
which obliterates the command is quite simple.

#include <stdio.h>
#include <string.h>

#define MAX_ARGS 10

int spawn(char *program, char **arg_list)
{
int i;
printf("spawning %s:\t", program);
i = 0;
for (i = 0; arg_list != 0; ++i) {
printf("%s, ", arg_list);
}
return i;
}

int main(void)
{
char test[] = "ls -al | grep abc";
char *arg_list[MAX_ARGS+1];
int i;
char *p;
p = strtok(test, " \t");
for (i = 0; i < MAX_ARGS; ++i) {
arg_list = p;
p = strtok(NULL, " \t");
}
arg_list = 0;
spawn(arg_list[0], arg_list);
puts("Finis.");
return 0;
}

But I encourage you to try to do better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,568
Members
47,204
Latest member
abhinav72673

Latest Threads

Top