lost. Help

Discussion in 'C Programming' started by Bill Cunningham, Apr 10, 2008.

  1. In a previous post I mentioned a beautifully working simple function
    called out with this prototype:

    int out (int n,char *buffer);

    Great. But how would I handle the command line? I have arrays and sub-arrays
    char *argv[] and argc.

    int main (int argc, char *argv[]) {
    /* In one of these two parameters I am going to have to take a
    string and send it back. What I want is a perfect version of puts. Which of
    these two parameters would take a single string like "hello world\n" and
    print it adding newline like puts ? I'm going to need extra functions. Maybe
    atoi and others. */

    return puts(string); /* from argc or argv[] */

    Am I making sense ?

    Bill
     
    Bill Cunningham, Apr 10, 2008
    #1
    1. Advertising

  2. Bill Cunningham

    Lew Pitcher Guest

    Bill Cunningham wrote:

    > In a previous post I mentioned a beautifully working simple function
    > called out with this prototype:
    >
    > int out (int n,char *buffer);


    Nice prototype. What sort of data do 'n' and 'buffer' represent?

    > Great. But how would I handle the command line? I have arrays and
    > sub-arrays char *argv[] and argc.
    >
    > int main (int argc, char *argv[]) {
    > /* In one of these two parameters I am going to have to take a
    > string


    Only one of those parameters is capable of presenting a "string".

    > and send it back.


    Back where?


    > What I want is a perfect version of puts. Which
    > of these two parameters would take a single string like "hello world\n"
    > and print it adding newline like puts ? I'm going to need extra functions.
    > Maybe atoi and others. */
    >
    > return puts(string); /* from argc or argv[] */
    >
    > Am I making sense ?


    No and yes.

    You want one of the occurrences of argv[]. /Which/ occurrence depends on
    a) the order of argument data as provided by the environment (the "command
    line" or it's equivalent, and your "usage" for your program), and
    b) the value of argc

    argc will contain a count of the number of valid argv[] elements, or 0

    if argc > 0
    argv[0] will be a pointer to a string containing the "program name",
    whatever /that/ is
    argv[1] through argv[argc-1] will be pointers to strings, each string
    being provided by your execution environment. Typically, argv[1] is
    the first string provided on the commandline, argv[2] is the 2nd, etc.
    argv[argc] will be NULL

    For example, using the code below...

    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    printf("argc == %d\n",argc);

    if (argc == 0)
    puts("Execution environment does not support argument strings");
    else
    {
    int arg;

    for (arg = 0; arg <= argc; ++arg)
    {
    printf("argv[%d] = ",arg);
    if (argv[arg] == NULL)
    puts("NULL");
    else
    printf("\"%s\"\n",argv[arg]);
    }
    }

    return EXIT_SUCCESS;
    }

    In my execution environment, we see that

    ~/code/tmp $ args
    argc == 1
    argv[0] = "args"
    argv[1] = NULL


    And that

    ~/code/tmp $ args a b c "d E fgh" i jkl
    argc == 7
    argv[0] = "args"
    argv[1] = "a"
    argv[2] = "b"
    argv[3] = "c"
    argv[4] = "d E fgh"
    argv[5] = "i"
    argv[6] = "jkl"
    argv[7] = NULL


    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, Apr 11, 2008
    #2
    1. Advertising

  3. > Nice prototype. What sort of data do 'n' and 'buffer' represent?
    >
    >> Great. But how would I handle the command line? I have arrays and
    >> sub-arrays char *argv[] and argc.
    >>
    >> int main (int argc, char *argv[]) {
    >> /* In one of these two parameters I am going to have to take a
    >> string

    >
    > Only one of those parameters is capable of presenting a "string".
    >
    >> and send it back.

    >
    > Back where?
    >
    >
    >> What I want is a perfect version of puts. Which
    >> of these two parameters would take a single string like "hello world\n"
    >> and print it adding newline like puts ? I'm going to need extra
    >> functions.
    >> Maybe atoi and others. */
    >>
    >> return puts(string); /* from argc or argv[] */
    >>
    >> Am I making sense ?

    >
    > No and yes.
    >
    > You want one of the occurrences of argv[]. /Which/ occurrence depends on
    > a) the order of argument data as provided by the environment (the "command
    > line" or it's equivalent, and your "usage" for your program), and
    > b) the value of argc
    >
    > argc will contain a count of the number of valid argv[] elements, or 0
    >
    > if argc > 0
    > argv[0] will be a pointer to a string containing the "program name",
    > whatever /that/ is
    > argv[1] through argv[argc-1] will be pointers to strings, each string
    > being provided by your execution environment. Typically, argv[1] is
    > the first string provided on the commandline, argv[2] is the 2nd, etc.
    > argv[argc] will be NULL
    >
    > For example, using the code below...
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(int argc, char *argv[])
    > {
    > printf("argc == %d\n",argc);
    >
    > if (argc == 0)
    > puts("Execution environment does not support argument strings");
    > else
    > {
    > int arg;
    >
    > for (arg = 0; arg <= argc; ++arg)
    > {
    > printf("argv[%d] = ",arg);
    > if (argv[arg] == NULL)
    > puts("NULL");
    > else
    > printf("\"%s\"\n",argv[arg]);
    > }
    > }
    >
    > return EXIT_SUCCESS;
    > }
    >
    > In my execution environment, we see that
    >
    > ~/code/tmp $ args
    > argc == 1
    > argv[0] = "args"
    > argv[1] = NULL
    >
    >
    > And that
    >
    > ~/code/tmp $ args a b c "d E fgh" i jkl
    > argc == 7
    > argv[0] = "args"
    > argv[1] = "a"
    > argv[2] = "b"
    > argv[3] = "c"
    > argv[4] = "d E fgh"
    > argv[5] = "i"
    > argv[6] = "jkl"
    > argv[7] = NULL
    >
    >

    Lew alot of your code went over my head. Let me write a simple function
    of what I want.

    int out(char * buffer) {
    return puts(buffer);
    }

    Out just behaves as puts. Nice function. But I want to call from command
    line in linux enviornment.

    out "hello world" /*< stdin*/
    /*stdout>*/ hello world /*with \n from puts */

    So I know I will have to begin like this.

    int main (int argc,char **argv) {

    puts will have to be in the body somewhere and argc and argv control
    somewhere. Also since main returns int I think I would have to convert the
    char* into an int. It's what to do with argc and argv[] that I don't know
    what to do.

    Bill
     
    Bill Cunningham, Apr 11, 2008
    #3
  4. > Lew alot of your code went over my head. Let me write a simple function
    > of what I want.
    >

    Ok you're demonstrating how argc and argv works.

    Bill
     
    Bill Cunningham, Apr 11, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thanh Nguyen
    Replies:
    0
    Views:
    448
    Thanh Nguyen
    Apr 1, 2004
  2. =?Utf-8?B?aG4=?=

    Help! Browser history is lost in IE 6.0

    =?Utf-8?B?aG4=?=, May 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    564
    =?Utf-8?B?aG4=?=
    May 13, 2004
  3. Swanand Mokashi
    Replies:
    0
    Views:
    571
    Swanand Mokashi
    May 13, 2004
  4. Tonix
    Replies:
    0
    Views:
    424
    Tonix
    Aug 24, 2004
  5. Randy Smith
    Replies:
    3
    Views:
    379
    Randy Smith
    May 5, 2005
Loading...

Share This Page