My own argv with some modifications

This is a discussion on My own argv with some modifications within the C Programming forums, part of the General Programming Boards category; I am trying to write my own type of argv that puts arguments into a an array, commands[]. I want ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    My own argv with some modifications

    I am trying to write my own type of argv that puts arguments into a an array, commands[]. I want to use this in a menu shell in a program. An example:

    shell> create “an message with spaces”

    When typing this in the menu the command array should have length 2.

    command[0] = create
    command[1] = an message with spaces

    Sow when we have a sentences between “ and “ the code wont distinguish(create an new array element) when we have a space.

    How can we do that ?
    Code:
    	while (fgets(line, LINE_MAX, stdin) != NULL) {
    	    command = line;
    
    	}
    This code will read a line from the terminal, but how can I put the sentences in a array?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Depending on the OS... you may have to quote them.

    In windows My name is fred is interpreted as
    My
    name
    is
    fred

    If you put it in quotes it will see it as
    "My name is fred"... all once sentence.

    Of course you'll have to remove the quotes after copying it from your command line to the array.

    You put them in your array with strcpy() or similar. You're trying to copy a string, C doesn't know strings... so you need the library's string handlers to help you.
    Last edited by CommonTater; 10-11-2010 at 06:34 PM.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Thank you

    I use unix. I need some code to se how I can divide a sentence after spaces and taking care of quotes ?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tull2 View Post
    Thank you

    I use unix. I need some code to se how I can divide a sentence after spaces and taking care of quotes ?
    Yes you could spend your time looking for code and examples but if you really want to understand the language and it's power, you really should try to figure this out on your own as much as you can. Hit the man pages for your compiler, read a book (they do still make them, you know), grab a couple of tutorials... In the end you'll be glad you did.

    For now look up strtok() and memset()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with testing argv sum program
    By m&m in forum C Programming
    Replies: 5
    Last Post: 07-13-2010, 01:03 AM
  2. argc, argv
    By Martas in forum C Programming
    Replies: 6
    Last Post: 11-19-2009, 09:39 AM
  3. argv
    By taurus in forum C Programming
    Replies: 15
    Last Post: 10-14-2007, 09:57 AM
  4. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 06:09 AM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-09-2001, 12:04 AM

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