Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Argc and Argv

Reply
Thread Tools

Argc and Argv

 
 
Romulo Carneiro
Guest
Posts: n/a
 
      08-23-2007
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[i]);
}
fclose(fp);
system("type teste.txt");

return 0;
}

 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      08-23-2007
>"Romulo Carneiro" <(E-Mail Removed)> schrieb im Newsbeitrag
> >news:(E-Mail Removed) oups.com...

>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


 
Reply With Quote
 
 
 
 
Lars.Dahlback@gmail.com
Guest
Posts: n/a
 
      08-23-2007
On 23 Aug, 16:47, Romulo Carneiro <(E-Mail Removed)> 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[i]);}
>
> fclose(fp);
> system("type teste.txt");
>
> return 0;
>
>
>

Change the loop to:

for (int i = 0; i<argc; i++)

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-23-2007
Romulo Carneiro <(E-Mail Removed)> 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[i]);
> }


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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      08-23-2007
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[i]);
}
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[i]);
> }
> fclose(fp);
> system("type teste.txt");
>
> return 0;
> }
>

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      08-26-2007
On Thu, 23 Aug 2007 14:47:58 -0000, Romulo Carneiro
<(E-Mail Removed)> 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[i]);
>}


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Command line parameters - argc and argv[] ern C Programming 2 08-17-2006 04:13 PM
How to alter argc and argv Billy Patton C++ 5 06-03-2005 04:09 PM
use of argc and argv Claudio Varini C++ 3 10-28-2004 02:52 PM
argc and argv userfriendly C++ 3 03-01-2004 03:12 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM



Advertisments