473,395 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Argc and Argv

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
4 7161
>"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: mg | last post by:
Hello, I am writting bindings for a FEM application. In one of my function 'initModulename', called when the module is imported, I would like to get the argc and argv arguments used in the main...
9
by: Vinu | last post by:
Hai when i compile the cpp file(cmdargs.cpp) i attached the output below the program int main(int argc, wchar_t* argv) { std::wcout<<L"Name of the Program is "<<*argv<<std::endl;...
19
by: hpy_awad | last post by:
What should I do to avoid the message : parameter 'argc' is never used My program is down : *-------------------- /* Book name : The prodessional programmers guide to C File name ...
29
by: Merrill & Michele | last post by:
I'm now looking at page 115 K&R. After having discussed counterexamples, I shall herewith and henceforth make all my c programs look like int main(int orange, char* apple) {return(0);} Q1) ...
13
by: Sokar | last post by:
I have my main function set up as int main(int argv, char *argv) so taht i can read in a variable which is passed to the program on the command line. The problem is that main calls other...
4
by: Xog Blog | last post by:
Hi I am by some people's standards a newbie to C, and I am refreshing my memory as to some of its conventions. I would like any patient soul out there to help out. I am having a horrible time...
7
by: =?Utf-8?B?Vmlu?= | last post by:
Hi, I have a question. I created a simple executable program using Visual C++ from Visual Studio 6.0 This program is called from a script that passes in one argument. Now, my question is: ...
4
by: interec | last post by:
Hi Folks, I am writing a c++ program on redhat linux using main(int argc, wchar_t *argv). $LANG on console is set to "en_US.UTF-8". g++ compiler version is 3.4.6. Q1. what is the encoding of...
11
by: vicky | last post by:
hi all, please tell me with example, how the *argv point to the the no of strings.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.