I need some help with parsing a line of text. The line of text being parsed is user input.
I am trying to parse something that has a pipe in it.
For example: hello world | one two
I can get the words before the pipe to go into ArgList. But can't figure out how to get words after the pipe into ArgList2. The pipe symobol does not need to be stored anywhere.
Basically, how do I get the following?
ArgList[0] = hello
ArgList[1] = world
ArgList2[0] = one
ArgList2[1] = two
int main(void)
{
char *ArgList[MAX_ARG_LENGTH];
char *ArgList2[MAX_ARG_LENGTH];
char buf[MAX_BUF_LENGTH];
int i = 0
printf("> ");
if(!fgets(str, MAX_BUF_LENGTH, stdin))
perror("fgets error");
ArgList[i] = strtok(bufstr, " \n");
while(ArgList[i] != NULL)
{
printf("%s", ArgList[i]);
i++;
ArgList[i] = strtok(NULL, " \n");
}
return 0;
}
Any suggestions would be appreciated. Should I first tokenize the whole user input string into ArgList and then move everything after the pipe symbol into ArgList2?
What is the best way to go about this?