www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - args.length problem

reply jicman <jicman_member pathlink.com> writes:
Greetings D people!

Let us use this sample code:

import std.string;
int main(char[][] args)
{
char[] a = std.string.toString(args.length);
printf(a);
return (0);
}
after a successful compile, a run with different arguments print the weirdest
outcome!  ie.

No arguments:
14:57:36.62>test
123456789

4 arguments:
14:57:38.89>test 0 1 2 3
56789

So, is there an specific way to get the number of arguments given to a program?
I mean, I can get it from this, but I can anyone explain why this is printing
instead of 4?

thanks.

josé


test
Feb 18 2005
next sibling parent brad beveridge <brad nowhere.com> writes:
 import std.string;
 int main(char[][] args)
 {
 char[] a = std.string.toString(args.length);
 printf(a);
 return (0);
 }
A char[] is not the same as the char * that printf expects. printf("%.*s\n", a); worked fine for me. Brad
Feb 18 2005
prev sibling next sibling parent jicman <jicman_member pathlink.com> writes:
I've also tried this code:
import std.string;
int main(char[][] args)
{
if (args.length ==0) printf("0 argument\n");
if (args.length ==1) printf("1 argument\n");
if (args.length ==2) printf("2 arguments\n");
if (args.length ==3) printf("3 arguments\n");
if (args.length ==4) printf("4 arguments\n");
if (args.length ==5) printf("5 arguments\n");
char[] a = std.string.toString(args.length);
printf(a);
return (0);
}

and here is the output:

No arguments:
15:10:31.12>test
1 argument
123456789

So, every d program has at least one argument: the program itself. args[0].

here is with 1 argument:

15:10:34.09>test 1
2 arguments
23456789

Ok, never mind... I answered my own question. :-)

thanks.

josé

In article <cv5hj5$glo$1 digitaldaemon.com>, jicman says...
Greetings D people!

Let us use this sample code:

import std.string;
int main(char[][] args)
{
char[] a = std.string.toString(args.length);
printf(a);
return (0);
}
after a successful compile, a run with different arguments print the weirdest
outcome!  ie.

No arguments:
14:57:36.62>test
123456789

4 arguments:
14:57:38.89>test 0 1 2 3
56789

So, is there an specific way to get the number of arguments given to a program?
I mean, I can get it from this, but I can anyone explain why this is printing
instead of 4?

thanks.

josé


test
Feb 18 2005
prev sibling next sibling parent Nick <Nick_member pathlink.com> writes:
In article <cv5hj5$glo$1 digitaldaemon.com>, jicman says...
Greetings D people!

Let us use this sample code:

import std.string;
int main(char[][] args)
{
char[] a = std.string.toString(args.length);
printf(a);
return (0);
}
Don't use printf, use writefln. You need to import std.stdio first. (Then why is printf there you ask? Yes it's stupid, and yes we know :) Nick
Feb 18 2005
prev sibling parent "Regan Heath" <regan netwin.co.nz> writes:
On Fri, 18 Feb 2005 20:01:41 +0000 (UTC), jicman  
<jicman_member pathlink.com> wrote:
 Greetings D people!

 Let us use this sample code:

 import std.string;
 int main(char[][] args)
 {
 char[] a = std.string.toString(args.length);
 printf(a);
 return (0);
 }
 after a successful compile, a run with different arguments print the  
 weirdest
 outcome!  ie.

 No arguments:
 14:57:36.62>test
 123456789

 4 arguments:
 14:57:38.89>test 0 1 2 3
 56789

 So, is there an specific way to get the number of arguments given to a  
 program?
 I mean, I can get it from this, but I can anyone explain why this is  
 printing
 instead of 4?

 thanks.

 josé
In case you, or anyone else is interested, the reasons for your results: 1. The toString function is optimised to return a slice of a static string, the static string reads "0123456789". In the first case you were given a slice from 1 onward, of length 1. In the second you were given a slice from 5 onward of length 1. 2. Printf expects a C string, that is a pointer to a bunch of char's terminated with a null char, D strings are not null terminated, instead they have a length. 3. Static D strings are in fact null terminated, which is why you didn't get a bunch of garbage after the 9 in each case. So, to correctly print a D string using printf you use the method Brad has shown, or call toStringz to convert from a D string into a C string (i.e. add a null to the string). Or you can use the method Nick has suggested. Newcomers to D keep running into this, I suggest we move/remove printf from object.d and force people to import std.c.stdio to get it, then we ensure writef is documented (I'd rename std.stdio to std.io also). Regan
Feb 20 2005