473,789 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.tx t", "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 7187
>"Romulo Carneiro" <ro***********@ gmail.comschrie b im Newsbeitrag
news:11******** *************@x 35g2000prf.goog legroups.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.tx t", "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.tx t", "w"))) {
fprintf(stderr,
"Could not open teste.txt for output.\nQuitti ng\n");
exit(EXIT_FAILU RE);
}
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.tx t", "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.e xe 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.tx t", "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
7302
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 function of Python. So, my question is: does the Python API containe fonctions like 'get_argc()' and 'get_argv()' ? Thanks,
9
2848
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; std::wcout<<L" Argument Count "<<argc-1<<std::endl;
19
9608
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 : E:\programs\bc\witi01\ch10\main.c Program discription: TV rental system menu driven-ver01-W */
29
1883
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) How large is that int and that char*? (My instinct would be to look at limits.h, but with the main call, I can't figure out to what extent I'm %inside% C.)
13
7785
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 functions and some of them need to start jump to the start of main. If my main setup was just int main() this would be no problem, i could just call main(); The problem is main needs parameters passed to it now. Would any1
4
1924
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 figuring out argc/argv in a C program I am writing. I need a program to extract integers and print the ASCII representations of them. Here is the source of this very short program under Linux:
7
4819
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: 1. When I use int main(int argc, char* argv) declaration, the argv usually
4
9801
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 data that I get in argv ? Q2. what is encoding of string constants defined in programs (for example L"--count") ?
11
6063
by: vicky | last post by:
hi all, please tell me with example, how the *argv point to the the no of strings.
0
9663
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10136
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.