Solved

Parsing user entered commands

Posted on 2009-07-06
3
216 Views
Last Modified: 2013-12-26
Hi,
I am writing a program that takes an user input and sends it to execvp to execute. Right now I am stuck on parsing the user inputs. I think its a problem with my delimiter. if I use a different delimiter such as a comma it works fine, but I need to use a space. Take a look at tell me whats going on. Thanks.

Once I parse them how can I use each token separately? can I say pnt[0], pnt[1]? THanks
#include <stdio.h>
 
int main(int argc, char *argv[])
{
char comm[100];
char *pnt;
int i=0;
char delims[]= " ";
 
if (argc==1 ||  argc >2){
printf("Incorrect number of arguments. Exiting\n");
exit(0);
}
printf("%s ", argv[1]);
scanf("%s",&comm);
 
pnt = strtok (comm,delims);
 
while (pnt != NULL){
printf("%s\n",pnt);
pnt=strtok(NULL,delims);
}
}

Open in new window

0
Comment
Question by:wannabe_cpgeek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24790841
scanf() is only going to scan up until the first space, so  you really should use gets() to get the full line before trying to parse it.


Assuming:

red green blue


scanf("%s", ..) will scan "red" and leave the rest in the buffer (so you could call scanf again and get the next)

gets() will scan "red green blue", then you can use strtok() to parse
0
 

Author Comment

by:wannabe_cpgeek
ID: 24791052
Thanks. I figured it out.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I need help adding validation to my MVC.Net view 6 35
Is it possible to delete files one by one rather than the whole? 15 64
Problem to copy file 14 89
asp.net web app 3 58
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question