Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

When I run this program,it ouput incorrent result to the console prompt.

Moderators: kris

Posted: 05/24/07 00:11:12

import tango.stdc.stdio; int main(char[][] args) {

for (int i=0; i<args.length; i++) printf("%s\n",cast(char*)args[i]); char a[]="I love kxd."; printf("%s\n",cast(char *)a); return 0;

}

/* i use tango 0.98rc2 */

Author Message

Posted: 05/24/07 00:12:27

the output of args is wrong.

Posted: 05/24/07 18:38:18

Strings in D are not zero-terminated like they are in C, and casting to char* won't change that. If you want to use printf for any D string other than a string literal, you'll have to either use "%.*s" as the format string (with a normal D char array) or add the zero yourself. tango.stdc.stringz has routines to do this for you.

Posted: 05/25/07 10:46:03

But when i use dmd+phobos , it's can output correct answer to the command prompt.!

Posted: 05/25/07 11:06:16

Are you using writefln or printf with Phobos? For formatted output with Tango, you should use Stdout:

import tango.io.Stdout;

foreach (arg; args) {
    Stdout.format("{} ", arg);
    char[] a = "I love kxd";
    Stdout.format("{}", a);
}

See also the formatting tutorial.

Posted: 05/26/07 02:09:07

Even though,when i run it in console,like a.exe. when i run it in D:\,then if i use dmd+phobos compile it, then it output D:\a.exe. but if i use dmd+tango then the application output a if i use a command a.

Posted: 05/26/07 15:15:09

For the app:

import tango.stdc.stdio;

int main(char[][] args)
{
    for (int i=0; i<args.length; i++)
        printf("%s\n",cast(char*)args[i]);
    char a[]="I love kxd.";
    printf("%s\n",cast(char *)a);
    return 0;
}

When I run that against Tango I get:

C:\code\src\d\test>dmd test
c:\bin\dmd\bin\..\..\dm\bin\link.exe test,,,user32+kernel32/noi+tango.lib;

C:\code\src\d\test>test
test0☺
I love kxd.

C:\code\src\d\test>

When I change it to:

import tango.stdc.stdio;

int main(char[][] args)
{
    for (int i=0; i<args.length; i++)
        printf("%.*s\n",args[i]);
    char a[]="I love kxd.";
    printf("%.*s\n",a);
    return 0;
}

I get:

C:\code\src\d\test>dmd test
c:\bin\dmd\bin\..\..\dm\bin\link.exe test,,,user32+kernel32/noi+tango.lib;

C:\code\src\d\test>test
test
I love kxd.

C:\code\src\d\test>

This is expected. The first array is generated inside the runtime and does not contain a trailing null, so casting it to a char* and passing it to a function that expects a trailing null displays unpredictable behavior. The second array is a string literal and implicitly contains a trailing null "just in case." I'm not sure this is documented but it's how D seems to work.

As for why Tango is different than Phobos for the first test, it's because of how command-line arguments are processed in Tango vs. Phobos. Tango correctly handles non-ASCII command-line characters while Phobos does not. To do this, a bit of extra processing is done to the args as they are passed, transforming them into D strings in the process. Phobos, however, passes the C strings supplies by the OS straight into the app.

Posted: 05/28/07 05:18:58

When I use dmd+phobos and use the following source code:

import std.stdio;

int main(char[][] args)
{
    for (int i=0; i<args.length; i++)
        printf("%.*s\n",args[i]);
    char a[]="I love kxd.";
    printf("%.*s\n",a);
    return 0;
}

and compile it and run it like this:

C:\dmd\bin>dmd test.d
.\link.exe test,,,user32+kernel32/noi;

C:\dmd\bin>test
C:\dmd\bin\test.exe
I love kxd.

C:\dmd\bin>

You see, the first line of the output is different in dmd+phobos and dmd+tango.

In dmd+phobos, it output "C:\dmd\bin\test.exe".

But in dmd+tango, it output "test"

Posted: 06/09/07 20:49:25

Can anybody answer my question?

Posted: 06/09/07 22:39:52

Sean actually already gave the answer. To make it possible to pass unicode arguments to the program, Tango use a function in the Win32 API (I forget the name). This function has the sad deficiency that it removes the path of the exe name. We feel that proper unicode handling is more important though, and by using the function tango.sys.Environment.exePath on the exe name, you should get what you want anyway.