Argc and Argv

Discussion in 'C Programming' started by Romulo Carneiro, Aug 23, 2007.

  1. Hi,
    I'm programming in Windows XP and i'm trying to get all arguments of
    some application, but i only have gotten five argv. When i put more
    then five(5), it didn't display.
    =>Input Command Line:
    Argumentos.exe MyName Arg_1 Arg_2 Arg_3 Arg_3 Arg_4 Arg_5 Arg_6

    =>output of my program:
    Size of Argc:8
    Size of Argv: 4
    i:0 - Argumentos.exe
    i:1 - MyName
    i:2 - Arg_1
    i:3 - Arg_2
    i:4 - Arg_3

    =>Source code of my program:

    //Program to get the list of command line arguments.
    #include "stdafx.h"
    #include "stdio.h"
    #include "process.h"

    int main(int argc, char* argv[])
    {
    FILE *fp;

    // Criando uma aplicação
    //Open a File teste.txt
    fp = fopen("teste.txt", "w");
    //Print the
    fprintf( fp, "Size of Argc:%d\n", argc );
    int y = MemorySize(argv);
    //int y = sizeof(*argv);
    fprintf( fp, "Size of Argv: %d\n", y );
    int x = sizeof(argc);
    for (int i = 0; i<= y; i++)
    {
    fprintf(fp, "i:%d - %s\n", i, argv);
    }
    fclose(fp);
    system("type teste.txt");

    return 0;
    }
     
    Romulo Carneiro, Aug 23, 2007
    #1
    1. Advertising

  2. >"Romulo Carneiro" <> schrieb im Newsbeitrag
    > >news:...

    >Hi,
    >I'm programming in Windows XP and i'm trying to get all arguments of
    >some application, but i only have gotten five argv. When i put more
    >then five(5), it didn't display.

    ....
    >int x = sizeof(argc);

    argc is an int, which on your system happens to by 4 bytes big.
    >for (int i = 0; i<= y; i++)

    and here i gets the valies 0, 1,2,3 and 4...

    use "int x = argc;" instead (i.e. loose the sizeof)

    Bye, Jojo
     
    Joachim Schmitz, Aug 23, 2007
    #2
    1. Advertising

  3. Romulo Carneiro

    Guest

    On 23 Aug, 16:47, Romulo Carneiro <> wrote:
    > Hi,
    > I'm programming in Windows XP and i'm trying to get all arguments of
    > some application, but i only have gotten five argv. When i put more
    > then five(5), it didn't display.
    > =>Input Command Line:
    > Argumentos.exe MyName Arg_1 Arg_2 Arg_3 Arg_3 Arg_4 Arg_5 Arg_6
    >
    > =>output of my program:
    > Size of Argc:8
    > Size of Argv: 4
    > i:0 - Argumentos.exe
    > i:1 - MyName
    > i:2 - Arg_1
    > i:3 - Arg_2
    > i:4 - Arg_3
    >
    > =>Source code of my program:
    >
    > //Program to get the list of command line arguments.
    > #include "stdafx.h"
    > #include "stdio.h"
    > #include "process.h"
    >
    > int main(int argc, char* argv[])
    > {
    > FILE *fp;
    >
    > // Criando uma aplicação
    > //Open a File teste.txt
    > fp = fopen("teste.txt", "w");
    > //Print the
    > fprintf( fp, "Size of Argc:%d\n", argc );
    > int y = MemorySize(argv);
    > //int y = sizeof(*argv);
    > fprintf( fp, "Size of Argv: %d\n", y );
    > int x = sizeof(argc);
    > for (int i = 0; i<= y; i++)
    > {
    > fprintf(fp, "i:%d - %s\n", i, argv);}
    >
    > fclose(fp);
    > system("type teste.txt");
    >
    > return 0;
    >
    >
    >

    Change the loop to:

    for (int i = 0; i<argc; i++)
     
    , Aug 23, 2007
    #3
  4. Romulo Carneiro <> writes:
    [...]
    > //Program to get the list of command line arguments.
    > #include "stdafx.h"


    That's a non-standard header.

    > #include "stdio.h"


    stdio.h is standard, but you should use
    #include <stdio.h>

    > #include "process.h"


    That's a non-standard header.

    > int main(int argc, char* argv[])
    > {
    > FILE *fp;
    >
    > // Criando uma aplicação
    > //Open a File teste.txt
    > fp = fopen("teste.txt", "w");


    You should check whether the fopen() call failed. It's not a big deal
    in a toy program like this, but keep it in mind.

    > //Print the
    > fprintf( fp, "Size of Argc:%d\n", argc );


    You're printing the value of argc, not its size.

    C identifiers are case-sensitive; argc and Argc could be two distinct
    entities. Don't develop the habit of arbitrarily capitalizing
    identifiers, even for output.

    > int y = MemorySize(argv);


    There is no MemorySize function in standard C. I don't know what
    it's supposed to do.

    > //int y = sizeof(*argv);


    sizeof(*arg) is simply the size in bytes of what argv points to; since
    argv is a char**, sizeof(*argv) is the same as sizeof(char*). I don't
    see how this information is of any use to you in this program.

    > fprintf( fp, "Size of Argv: %d\n", y );
    > int x = sizeof(argc);
    > for (int i = 0; i<= y; i++)
    > {
    > fprintf(fp, "i:%d - %s\n", i, argv);
    > }


    What you want is the *value* of argc, not it size (argc, not
    sizeof(argc)).

    > fclose(fp);
    > system("type teste.txt");


    You're assuming there's a command called "type". There may not be
    such a command (or there might be a "type" command that does something
    entirely different).

    You could just write to stdout, but then you wouldn't also get the
    "teste.txt" file. If you want your program to be portable, you can
    write to "teste.txt" as you do here, then read it and write its
    contents to stdout. Or you could write each line of output both to fp
    and to stdout.

    > return 0;
    > }


    Incidentally, your program uses a number of features that are specific
    to C99. That's fine if you're using a compiler that supports C99, or
    at least enough of it for your purposes, but be aware that it could
    make your program difficult to port to other systems and
    implementations.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 23, 2007
    #4
  5. Romulo Carneiro wrote:
    > Hi,
    > I'm programming in Windows XP and i'm trying to get all arguments of
    > some application, but i only have gotten five argv. When i put more
    > then five(5), it didn't display.
    > =>Input Command Line:
    > Argumentos.exe MyName Arg_1 Arg_2 Arg_3 Arg_3 Arg_4 Arg_5 Arg_6
    >
    > =>output of my program:
    > Size of Argc:8
    > Size of Argv: 4
    > i:0 - Argumentos.exe
    > i:1 - MyName
    > i:2 - Arg_1
    > i:3 - Arg_2
    > i:4 - Arg_3


    [OP's code at EOM]

    Please examine the following. It is not bulletproof, so some may carp,
    but most of the salient points are addressed. Please note the difference
    in the headers included.

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


    int main(int argc, char *argv[])
    {
    FILE *fp;

    if (!(fp = fopen("teste.txt", "w"))) {
    fprintf(stderr,
    "Could not open teste.txt for output.\nQuitting\n");
    exit(EXIT_FAILURE);
    }
    fprintf(fp, "The size of argc (%zu) is irrelevant,"
    " its value is %d\n", sizeof(argc), argc);
    fprintf(fp, "Argv is a pointer,"
    " its size (%zu) is not relevant.\n", sizeof(argv));
    {
    int i;
    for (i = 0; i < argc; i++)
    fprintf(fp, "argv[%d] is \"%s\"\n", i, argv);
    }
    fclose(fp);
    system("cat teste.txt"); /* On my system 'type' needs to be
    'cat' (unless I alias it) */

    return 0;
    }


    $ Argumentos.exe MyName Arg_1 Arg_2 Arg_3 Arg_3 Arg_4 Arg_5 Arg_6

    The size of argc (4) is irrelevant, its value is 9
    Argv is a pointer, its size (4) is not relevant.
    argv[0] is "./Argumentos.exe"
    argv[1] is "MyName"
    argv[2] is "Arg_1"
    argv[3] is "Arg_2"
    argv[4] is "Arg_3"
    argv[5] is "Arg_3"
    argv[6] is "Arg_4"
    argv[7] is "Arg_5"
    argv[8] is "Arg_6"


    [EOM: OP's code]
    >
    > =>Source code of my program:
    >
    > //Program to get the list of command line arguments.
    > #include "stdafx.h"
    > #include "stdio.h"
    > #include "process.h"
    >
    > int main(int argc, char* argv[])
    > {
    > FILE *fp;
    >
    > // Criando uma aplicação
    > //Open a File teste.txt
    > fp = fopen("teste.txt", "w");
    > //Print the
    > fprintf( fp, "Size of Argc:%d\n", argc );
    > int y = MemorySize(argv);
    > //int y = sizeof(*argv);
    > fprintf( fp, "Size of Argv: %d\n", y );
    > int x = sizeof(argc);
    > for (int i = 0; i<= y; i++)
    > {
    > fprintf(fp, "i:%d - %s\n", i, argv);
    > }
    > fclose(fp);
    > system("type teste.txt");
    >
    > return 0;
    > }
    >
     
    Martin Ambuhl, Aug 23, 2007
    #5
  6. On Thu, 23 Aug 2007 14:47:58 -0000, Romulo Carneiro
    <> wrote:

    >Hi,
    >I'm programming in Windows XP and i'm trying to get all arguments of
    >some application, but i only have gotten five argv. When i put more
    >then five(5), it didn't display.
    >=>Input Command Line:
    >Argumentos.exe MyName Arg_1 Arg_2 Arg_3 Arg_3 Arg_4 Arg_5 Arg_6
    >
    >=>output of my program:
    >Size of Argc:8
    >Size of Argv: 4
    >i:0 - Argumentos.exe
    >i:1 - MyName
    >i:2 - Arg_1
    >i:3 - Arg_2
    >i:4 - Arg_3
    >
    >=>Source code of my program:
    >
    >//Program to get the list of command line arguments.
    >#include "stdafx.h"
    >#include "stdio.h"
    >#include "process.h"
    >
    >int main(int argc, char* argv[])
    >{
    >FILE *fp;
    >
    >// Criando uma aplicação
    >//Open a File teste.txt
    >fp = fopen("teste.txt", "w");
    >//Print the
    >fprintf( fp, "Size of Argc:%d\n", argc );


    You are printing the value of argc, not its size.

    >int y = MemorySize(argv);
    >//int y = sizeof(*argv);
    >fprintf( fp, "Size of Argv: %d\n", y );
    >int x = sizeof(argc);
    >for (int i = 0; i<= y; i++)
    >{
    > fprintf(fp, "i:%d - %s\n", i, argv);
    >}


    The value of y does not change with the number of arguments. y is
    therefore a very poor choice to use as a limit when looping through
    argv. If the number of arguments is less than sizeof(char*), you will
    invoke undefined behavior on (at least) the last loop iteration. If
    the number of arguments is greater, you will ignore the remaining
    ones. Your limiting expression should be i < argc.

    >fclose(fp);
    >system("type teste.txt");
    >
    >return 0;
    >}



    Remove del for email
     
    Barry Schwarz, Aug 26, 2007
    #6
    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. userfriendly

    argc and argv

    userfriendly, Feb 29, 2004, in forum: C++
    Replies:
    3
    Views:
    6,008
    Buster
    Mar 1, 2004
  2. Claudio Varini

    use of argc and argv

    Claudio Varini, Oct 28, 2004, in forum: C++
    Replies:
    3
    Views:
    2,143
    Howard
    Oct 28, 2004
  3. Billy Patton

    How to alter argc and argv

    Billy Patton, May 31, 2005, in forum: C++
    Replies:
    5
    Views:
    859
    red floyd
    Jun 3, 2005
  4. Hal Styli
    Replies:
    14
    Views:
    1,646
    Old Wolf
    Jan 20, 2004
  5. ern
    Replies:
    2
    Views:
    431
    Walter Roberson
    Aug 17, 2006
Loading...

Share This Page