0

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?

1
  • 1
    What happens when you run this code? Commented Aug 5, 2011 at 0:29

2 Answers 2

3

I would do something like this:

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

int main(void)
{

char buf[30] = "hello world | one two";

char *str = buf;

char *str1 = strsep(&str, "|");
char *str2 = strsep(&str, "\n");

printf("\nstr1 = %s and str2 =%s \n", str1, str2);
return 0;
}

NOTE: you should now figure out how you want things your way.

Sign up to request clarification or add additional context in comments.

5 Comments

strsep is not ANSI C. Also, this doesn't do what the OP wants.
@Oli Charlesworth: Did OP ask for something specifically for ANCI C? I do not think so. I just gave him and idea and you should have read the NOTE.
It's a perfectly reasonable argument... Unless the OP specifies otherwise, you should assume they want a platform-agnostic solution. If the OP happens to be on Windows, your answer is of no use!
Thanks Hari, this seemed to do the trick for me. Your code split up the buffer into two strings. And then I tokenized the two strings separately, str1 into ArgList and str2 into ArgList2.
Oh and sorry for not specifying what my platform was. I was using Unix C. I guess strsep wouldn't have worked in Windows.
3

Try using scanf().

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

int main()
{
    char args1[NUMBER_OF_RECORDS][MAX_BUF_LENGTH];
    char args2[NUMBER_OF_RECORDS][MAX_BUF_LENGTH];
    char buf[MAX_BUF_LENGTH];
    char buf2[MAX_BUF_LENGTH];
    int i = 0;

    while (scanf("%s | %s", buf1, buf2)) {
        strcpy(args1[i], buf1);
        strcpy(args2[i], buf2);
        i++;
    }
    /* Do something with args1 or args2 */
    return 0;

}

No guarantees that this code works 100% correctly. Also, scanf() is prone to errors, so use it with caution.

1 Comment

scanf() is fine, but it has a bug in it where the first scanf() call will be fine, but if the user has pressed Enter at stdin, scanf() doesn't clear the buffer correctly and the second variable will be a carriage return. You can get around it by putting in a newline character like so: scanf("\n%d",&a); It doesn't seem like you'll necessarily need to use it, but it's good to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.