By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,495 Members | 1,320 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,495 IT Pros & Developers. It's quick & easy.

Argc and Argv

P: n/a
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;
}

Aug 23 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
>"Romulo Carneiro" <ro***********@gmail.comschrieb im Newsbeitrag
news:11*********************@x35g2000prf.googlegro ups.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
Aug 23 '07 #2

P: n/a
On 23 Aug, 16:47, Romulo Carneiro <romulo.fer...@gmail.comwrote:
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++)

Aug 23 '07 #3

P: n/a
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;
}
Aug 23 '07 #4

P: n/a
On Thu, 23 Aug 2007 14:47:58 -0000, Romulo Carneiro
<ro***********@gmail.comwrote:
>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
Aug 26 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.