Thread: command line argv argc help?

  1. #1
    Unregistered
    Guest

    Lightbulb command line argv argc help?

    how do i access the array within argv, this is what i have so far
    say if i typed this at the command prompt: hw6 -fl john smith 12/20/2001, it should go to the word smith and capitalize the first letter and prints it Smith. Also, loops the whole thing and do it again and again if there are more then 1 name.

    if (((cOption1 == FLCAPITALIZED) && (cOption2 == LASTNAME)) ||
    ((cOption2 == FLCAPITALIZED) && (cOption1 == LASTNAME))) {

    do {

    a = x * j;
    last[0] = ((argv[a][1]) - 32);

    for (i = 2; i < 40; i++) {
    last[i] = (argv[a][i]);

    }

    printf ("%s\n", last[i]);

    j++;

    } while ((argv[a+2]) != '\0');

    }

    Thank You

  2. #2
    Unregistered
    Guest
    anyother way i thought of is, i have

    int formatName (char *cpFirstName, char *cpLastName, char *cpFullName);

    and being passing the arguments

    formatName (argv[y], argv[p], u);

    it will return u as the whole full name as Smith, John.

    How do i extract the word Smith out from u?

    Thanks

  3. #3
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    >How do i extract the word Smith out from u?

    If you know the name is written as Smith, John then you can just copy the string into another string until you come across a comma. Or you can copy the name to another string until you come across a space:
    Code:
    while(Pos<strlen(String)&&String[Pos]!=','){
         New[Loc++] = String[Pos++];
    }
    New[Loc] = '\0';
    This will load from String to New until it comes across a comma.

    >how do i access the array within argv?

    argv[0] is the first string. The first letter in the first string is argv[0][0]. argc tells you how many strings are present and the strlen(argv[0]) will tell you how many charectors are in the first word in the command line.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  4. #4
    Unregistered
    Guest
    thanks sean, but my problem is i don't know what the name will be, john smith is just an example.

    can you also explain more about

    code:--------------------------------------------------------------------------------
    while(Pos<strlen(String)&&String[Pos]!=','){
    New[Loc++] = String[Pos++];
    }
    New[Loc] = '\0';
    --------------------------------------------------------------------------------

    also thanks, for explaining how to use argv and argc, but what i am trying to do is like for example smith is argv[4] and within that is the word smith, how do i access that a CAP the S and print the rest.

  5. #5
    Unregistered
    Guest

    Unhappy

    anothing thing is i can't use the string library to do this.... :-(

  6. #6
    Unregistered
    Guest

    Unhappy

    this is what i came up with so far, but it freezes and won't do anything, please help

    do {

    p = y + 1;

    formatName (argv[y], argv[p], u);

    for (i = 0; i < 40; i++) {

    while (u[i] != ',') {

    last[i] = u[i];

    }
    }

    printf ("%s", last);

    for (i=0; i < 40; i++) {

    u[i] = SPACE;

    }

    y = y + 3;

    } while ((argv[p+2]) != '\0');

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Does this help explain?
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	int i;
    	
    	/* remember, argv[0] is the program name */
    	for (i = 1; i < argc; i++)
    		printf("Arg %d is %s, the first char is %c\n", i, argv[i], argv[i][0]);
    	
    	return (0);
    }
    
    /* 
    c:\>junk1 my name is john
    Arg 1 is my, the first char is m
    Arg 2 is name, the first char is n
    Arg 3 is is, the first char is i
    Arg 4 is john, the first char is j
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help getting a grib on argc and argv
    By elwad in forum C Programming
    Replies: 10
    Last Post: 05-01-2009, 10:13 AM
  2. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 06:09 AM
  3. Converting a argc / argv construct into a va_list
    By chambece in forum C Programming
    Replies: 6
    Last Post: 07-03-2005, 04:47 PM
  4. argc & argv
    By miryellis in forum C Programming
    Replies: 11
    Last Post: 09-19-2004, 11:59 PM
  5. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM