Argc and Argv

R

Romulo Carneiro

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;
}
 
J

Joachim Schmitz

Romulo Carneiro said:
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
 
L

Lars.Dahlback

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++)
 
K

Keith Thompson

Romulo Carneiro said:
//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 said:
#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.
 
M

Martin Ambuhl

Romulo said:
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;
}
 
B

Barry Schwarz

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top