Parsing user entered commands

Posted on 2009-07-06
Medium Priority
Last Modified: 2013-12-26
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");
printf("%s ", argv[1]);
pnt = strtok (comm,delims);
while (pnt != NULL){

Open in new window

Question by:wannabe_cpgeek
LVL 40

Accepted Solution

mrjoltcola earned 2000 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.


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

Author Comment

ID: 24791052
Thanks. I figured it out.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

627 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