Detect "Exit" as first word

This is a discussion on Detect "Exit" as first word within the C Programming forums, part of the General Programming Boards category; Hi im making a shell program that reads a line of input from the user, then if "cont" is detected ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    13

    Detect "Exit" as first word

    Hi im making a shell program that reads a line of input from the user, then if "cont" is detected as the first word of the line inputted, the line will be tokenized.

    here's my main code
    Code:
    main ()
    {
    char line [ MAXLINE ], * words [ MAXWORDS ], message [ MAXLINE ];
    int stop =0 , nwords =0;
    while (1)
    {
    printf (" Enter line: " );
    if ( NULL == fgets ( line , MAXLINE , stdin ))
    return 0;
    tokenize ( line , words ,& nwords );
    }
    }
    here is my tokenize code:
    Code:
    void tokenize ( char * line , char ** words , int * nwords )
    {
    	* nwords =1;
    	for ( words [0]= strtok ( line , " \t\n" );
    	     (*nwords < MAXWORDS )&&( words[*nwords ]= strtok ( NULL , " \t\n" ));
    	     * nwords =* nwords +1);
    	return ;
    }
    I'm not sure how to detect if "cont" is appeared as first word of the line, then tokenize the line.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Grab the first word with sscanf and use strcmp to see if it is what you want it to be.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    Quote Originally Posted by quzah View Post
    Grab the first word with sscanf and use strcmp to see if it is what you want it to be.


    Quzah.
    thanks for the reply. I added the sscanf :
    Code:
    main ()
    {
    	char line [ MAX_LINE ] , * words [ MAX_WORDS ] , message [ MAX_LINE ];
    	int stop =0 , nwords =0; 
        int i; 
    	
    	while (1)
    	{
    		printf ( " Enter line:" );
    		if ( NULL == fgets ( line , MAX_LINE , stdin ))
    			return 0;
    		tokenize ( line , words ,& nwords );
    		sscanf(words, "cont", i);
    }
    }
    I'm not sure if i did it correctly, and how do i use strcmp to compare the words so that when "cont" is appeared as first word, tokenization will then take place?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Have you ever actually used scanf before? What is it you think you are storing here:
    Code:
    sscanf(words, "cont", i);
    You should read the man page (click that) if you aren't sure how it works. Do the same for strcmp.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    Quote Originally Posted by quzah View Post
    Have you ever actually used scanf before? What is it you think you are storing here:
    Code:
    sscanf(words, "cont", i);
    You should read the man page (click that) if you aren't sure how it works. Do the same for strcmp.


    Quzah.
    Hi thanks again for the reply. I never used scanf before. It's also the first time I've ever program with C using Linux, I used to do C++ programming in Windows but I found out they are quite different.
    I'll have a look at the website you gave me. Thanks again!

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    I now need to use fork() and Use one of the exec() family to run the program requested which is the Inputting and tokenizing lines I wrote above. But I'm not sure how to put the code into the exec function so that it runs the program. I have no problem making the fork(), it's just the child process that contains exec I'm havin problem with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc compile errors, HELP!
    By FalconGK81 in forum C Programming
    Replies: 11
    Last Post: 02-14-2011, 04:17 AM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 08:55 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21