www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How to printf __FILE__ ?

reply AEon <AEon_member pathlink.com> writes:
How does one make the below __VAR__ info usable in some way?

---------------------------
import std.c.stdio;
// import std.file;

int main (char[][] args)
{

printf("   Testing the D Code\n");

//	test.d(9): undefined identifier __FILE__
//	make: *** [test.o] Error 1
printf("   File:		%s\n",__FILE__);
printf("   Line:		%s\n",__LINE__);
printf("   Date:		%s\n",__DATE__);
printf("   Time: 		%s\n",__TIME__);
printf("   TimeStamp:	%s\n",__TIMESTAMP__);

return 0;
}
---------------------------

Is there some place where I could look up such things. Where to ask "newbie"
questions?

I am aware of http://www.digitalmars.com/d/index.html, but the docs are at times
without examples.

Presently I am testing many of the things in the docs, and spamming this forum
is probable not the best idea?!

AEon
Mar 14 2005
next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
AEon wrote:

 //	test.d(9): undefined identifier __FILE__
 //	make: *** [test.o] Error 1

What version of DMD are you running ? You need DMD version 0.116 or greater: http://www.digitalmars.com/d/changelog.html#new0116
 Is there some place where I could look up such things. Where to ask "newbie"
 questions?

We definitely need a new forum for such questions. digitalmars.D.beginners, or something. It is bound to get more visitors when D gets more actual users, as opposed to fellow language hackers and lawyers ?
 I am aware of http://www.digitalmars.com/d/index.html, but the docs are at
times
 without examples.

See also http://prowiki.org/wiki4d/wiki.cgi --anders
Mar 14 2005
next sibling parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
 We definitely need a new forum for such questions.

 digitalmars.D.beginners, or something. It is bound
 to get more visitors when D gets more actual users,
 as opposed to fellow language hackers and lawyers ?

Agreed. digitalmars.D.howto would be better I think. For all kind of howto's: how to compile, debug, implement, etc. Andrew. "Anders F Björklund" <afb algonet.se> wrote in message news:d14q1n$2baj$1 digitaldaemon.com...
 AEon wrote:

 // test.d(9): undefined identifier __FILE__
 // make: *** [test.o] Error 1

What version of DMD are you running ? You need DMD version 0.116 or greater: http://www.digitalmars.com/d/changelog.html#new0116
 Is there some place where I could look up such things. Where to ask 
 "newbie"
 questions?

We definitely need a new forum for such questions. digitalmars.D.beginners, or something. It is bound to get more visitors when D gets more actual users, as opposed to fellow language hackers and lawyers ?
 I am aware of http://www.digitalmars.com/d/index.html, but the docs are 
 at times
 without examples.

See also http://prowiki.org/wiki4d/wiki.cgi --anders

Mar 14 2005
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Andrew Fedoniouk wrote:

 digitalmars.D.beginners, or something.


 digitalmars.D.howto would be better I think.
 
 For all kind of howto's: how to compile, debug, implement, etc.

Hopefully those would be covered in the documentation ? http://www.prowiki.org/wiki4d/wiki.cgi?D__Tutorial/InstallingDCompiler http://www.prowiki.org/wiki4d/wiki.cgi?D__Tutorial/StartingWithD Wouldn't the forum be more for: "it doesn't work! help!" --anders
Mar 14 2005
parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
 Hopefully those would be covered in the documentation ?

Programmers (rrreal ones) do not read this stuff anyway. BTW: Original question starts from "How to..." so is a first candidate for howto. "Anders F Björklund" <afb algonet.se> wrote in message news:d14rl7$2baj$5 digitaldaemon.com...
 Andrew Fedoniouk wrote:

 digitalmars.D.beginners, or something.


 digitalmars.D.howto would be better I think.

 For all kind of howto's: how to compile, debug, implement, etc.

Hopefully those would be covered in the documentation ? http://www.prowiki.org/wiki4d/wiki.cgi?D__Tutorial/InstallingDCompiler http://www.prowiki.org/wiki4d/wiki.cgi?D__Tutorial/StartingWithD Wouldn't the forum be more for: "it doesn't work! help!" --anders

Mar 14 2005
parent "Walter" <newshound digitalmars.com> writes:
"Andrew Fedoniouk" <news terrainformatica.com> wrote in message
news:d14saa$2fie$1 digitaldaemon.com...
 Hopefully those would be covered in the documentation ?

Programmers (rrreal ones) do not read this stuff anyway. BTW: Original question starts from "How to..." so is a first candidate

 howto.

Feature requests always start out with "it would be nice if..." <g>.
Mar 14 2005
prev sibling parent reply AEon <AEon_member pathlink.com> writes:
Anders says...

What version of DMD are you running ?

I am running the latest Cygwin gdc 3.3.3-3 supposedly. The above did not work with DMD (also 3.3.3 AFAICT) either though. Derek Parnell says...
 import std.stdio;

$ dmd test.d Error: Error reading file 'std/stdio.d' Looks like the Cgywin install is not complete? Seems like the Cygwin distri is not as complete as it should be (or I simply forgot to install something), meaning if I want to properly code in D under windows I will need to install DMD? Thanx for the quick help. AEon
Mar 14 2005
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
AEon wrote:

What version of DMD are you running ? 

I am running the latest Cygwin gdc 3.3.3-3 supposedly. The above did not work with DMD (also 3.3.3 AFAICT) either though.

Sorry, but GDC 0.10 is only like DMD 0.110 __FILE__ and $ are not available in GDC yet. --anders PS. The "dmd" script from the GDC distribution is just a "gdc" wrapper, syntax-converting
Mar 14 2005
parent reply AEon <AEon_member pathlink.com> writes:
Thanx for all the feedback, I have installed DMD 0.118 and with the mentioned
links, installation was no problem. The code compiles now. A few questions are
still left though:

import std.c.stdio;
int main (char[][] args)
{
// DMD 0.118 
// -> File:                Error: Access Violation
printf("   File:		%s\n",__FILE__);
return 0;
}

How come printf does not take __FILE__ well?

As was suggested:

import std.stdio;
int main (char[][] args)
{
writefln("   File:      %s",__FILE__);
writefln("   Line:      %s",__LINE__);
writefln("   Date:      %s",__DATE__);
writefln("   Time:      %s",__TIME__);
writefln("   TimeStamp: %s",__TIMESTAMP__);
// With DMD 0.118
//   File:      test.d
//   Line:      54
//   Date:      Mar 14 2005
//   Time:      23:12:31
//   TimeStamp: Mon Mar 14 23:12:31 2005
return 0;
} 

Works just fine.

And why use writefln() (other than for the obvious reason printf crashes on the
above)? Is printf() somehow inherantly instable?

IOW what are the situtation where one should/can use printf() and when
writefln()?

Thanx.

AEon
Mar 14 2005
next sibling parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Mon, 14 Mar 2005 22:22:08 +0000 (UTC), AEon <AEon_member pathlink.com>  
wrote:
 Thanx for all the feedback, I have installed DMD 0.118 and with the  
 mentioned
 links, installation was no problem. The code compiles now. A few  
 questions are
 still left though:

 import std.c.stdio;
 int main (char[][] args)
 {
 // DMD 0.118
 // -> File:                Error: Access Violation
 printf("   File:		%s\n",__FILE__);
 return 0;
 }

 How come printf does not take __FILE__ well?

I assume it's because __FILE__ is a D string, and not a null-terminated C string. See: http://www.prowiki.org/wiki4d/wiki.cgi?ShortFrequentAnswers "Strings are not null-terminated but hold explicit length information. Therefore you need to use %.*s not %s in printf, or just use writef!" Regan
Mar 14 2005
parent reply AEon <AEon_member pathlink.com> writes:
In article <opsnngzocg23k2f5 nrage.netwin.co.nz>, Regan Heath says...

 // -> File:                Error: Access Violation
 printf("   File:		%s\n",__FILE__);


I assume it's because __FILE__ is a D string, and not a null-terminated C  
string.

Right, I had no real idea what type __FILE__ was. Strangely: printf("Line: %.*s\n",__LINE__); yields: "Line: (null)", whereas: writefln("Line: %s",__LINE__); yields: "Line: 80", some valid number. Did I stumble on some known bug? Or am I missing some special case? anders,
 printf(  "File:  %.*s\n", __FILE__ );
 printf(  "File:  %s\n",   std.string.toStringz(__FILE__) );
 writefln("File:  %s",     __FILE__ );

thanx for the explicit examples, those really help, i.e. how to force a D string to be a "C string". I had not fully understood the naming of writefln, the "ln" saving you a \n. Neat! This forum is great, should get me D coding in a jiffy :) AEon
Mar 14 2005
parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Tue, 15 Mar 2005 00:00:56 +0000 (UTC), AEon <AEon_member pathlink.com>  
wrote:
 In article <opsnngzocg23k2f5 nrage.netwin.co.nz>, Regan Heath says...

 // -> File:                Error: Access Violation
 printf("   File:		%s\n",__FILE__);


 I assume it's because __FILE__ is a D string, and not a null-terminated  
 C
 string.

Right, I had no real idea what type __FILE__ was. Strangely: printf("Line: %.*s\n",__LINE__); yields: "Line: (null)", whereas: writefln("Line: %s",__LINE__); yields: "Line: 80", some valid number. Did I stumble on some known bug? Or am I missing some special case?

__LINE__ is not a char[] it is an integer type perhaps 'int' eg. printf("%d\n",__LINE__); I am not sure why: writefln("Line: %s",__LINE__); works, writefln must be doing something clever. Regan
Mar 14 2005
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Tue, 15 Mar 2005 13:12:24 +1300, Regan Heath wrote:

 On Tue, 15 Mar 2005 00:00:56 +0000 (UTC), AEon <AEon_member pathlink.com>  
 wrote:
 In article <opsnngzocg23k2f5 nrage.netwin.co.nz>, Regan Heath says...

 // -> File:                Error: Access Violation
 printf("   File:		%s\n",__FILE__);


 I assume it's because __FILE__ is a D string, and not a null-terminated  
 C
 string.

Right, I had no real idea what type __FILE__ was. Strangely: printf("Line: %.*s\n",__LINE__); yields: "Line: (null)", whereas: writefln("Line: %s",__LINE__); yields: "Line: 80", some valid number. Did I stumble on some known bug? Or am I missing some special case?

__LINE__ is not a char[] it is an integer type perhaps 'int' eg. printf("%d\n",__LINE__); I am not sure why: writefln("Line: %s",__LINE__); works, writefln must be doing something clever.

It does. With printf, the coder has to ensure that the formatting codes match up with the data type passed for each respective argument. However, writef knows the data types of the arguments and does things accordingly. For example, you can also write ... writefln("Line: ", __LINE__); or writefln("Line: %d", __LINE__); to get the same results. -- Derek Melbourne, Australia 15/03/2005 11:28:43 AM
Mar 14 2005
parent reply AEon <AEon_member pathlink.com> writes:
Derek Parnell wrote:

For example, you can also write ...
  writefln("Line: ", __LINE__);
or 
  writefln("Line: %d", __LINE__);
to get the same results.

Indeed. Both work, you can even do this (as was pointed out, writefln is *very* flexible), though the formatting would be less than optimal: // Line: test.d 96Mar 15 200511:17:12Tue Mar 15 11:17:12 2005 writefln("Line: %d ", __FILE__,__LINE__,__DATE__,__TIME__,__TIMESTAMP__); // Line: test.d97Mar 15 200511:17:12Tue Mar 15 11:17:12 2005 writefln("Line: ", __FILE__,__LINE__,__DATE__,__TIME__,__TIMESTAMP__); I did a full search in the dmd\ development tree for "__TIMESTAMP__", other than in the dmd, and dmd.exe files, the only source related mention of the latter is in E:\d\dmd\src\dmd\idgen.c struct Msgtable { char *ident; // name to use in DMD source char *name; // name in D executable }; Msgtable msgtable[] = { { "LINE", "__LINE__" }, { "FILE", "__FILE__" }, { "DATE", "__DATE__" }, { "TIME", "__TIME__" }, { "TIMESTAMP", "__TIMESTAMP__" }, .. } Would there have been any way to look up what type "__LINE__" is for printf? I had assumed that simply all those __ vars are strings (as we found out D Strings to be exact)? PS: Any chance to fix this forum to actually respect indentations, to make the reading of source samples easier to read? AEon
Mar 15 2005
next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
AEon wrote:

 Would there have been any way to look up what type "__LINE__" is for printf? I
 had assumed that simply all those __ vars are strings (as we found out D
Strings
 to be exact)?

Sortof... This program:
 import std.stdio;
 void main()
 {
   writefln("__FILE__ ", typeid(typeof(__FILE__)));
   writefln("__LINE__ ", typeid(typeof(__LINE__)));
   writefln("__DATE__ ", typeid(typeof(__DATE__)));
   writefln("__TIME__ ", typeid(typeof(__TIME__)));
 }

*Should* print the types, but: __FILE__ TypeInfo __LINE__ long __DATE__ TypeInfo __TIME__ TypeInfo It seems broken for char[]... ? --anders
Mar 15 2005
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
 import std.stdio;
 void main()
 {
   writefln("__FILE__ ", typeid(typeof(__FILE__)));
   writefln("__LINE__ ", typeid(typeof(__LINE__)));
   writefln("__DATE__ ", typeid(typeof(__DATE__)));
   writefln("__TIME__ ", typeid(typeof(__TIME__)));
 }

*Should* print the types, but: __FILE__ TypeInfo __LINE__ long __DATE__ TypeInfo __TIME__ TypeInfo It seems broken for char[]... ?

Duh, forgot that string literals are of an unnamed type... They're converted into char[]/wchar[]/dchar[] from context. In this case, there was no context, so they renamed unnamed.
 import std.stdio;
 void main()
 {
   writefln("%s", typeid(typeof("")));
   writefln("%s", typeid(typeof(cast( char[]) "")));
   writefln("%s", typeid(typeof(cast(wchar[]) "")));
   writefln("%s", typeid(typeof(cast(dchar[]) "")));
 }

TypeInfo char[] wchar[] dchar[] Not a bug. *However*, string literals in D should all be zero-terminated... These new ones are not, and that's a genuine implementation bug. --anders
Mar 15 2005
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
(continuing talking to myself, thanks for listening :-) )

 *However*, string literals in D should all be zero-terminated...
 These new ones are not, and that's a genuine implementation bug.

Never mind, they are all zero-terminated. (sorry, Walter!) Just "forgot" that when passed as variadic arguments they are dynamic arrays and not pointers as when passed to the first argument of printf ("char*") for instance... Thus: printf(__FILE__); // works and: printf("%s\n", __FILE__); // segfaults but: printf("%.*s\n", __FILE__); // works printf("%s\n", cast(char*) __FILE__); // works But it's easier to use writef, which does the right thing. Regular string literal and printf/writef handling, in D... --anders
Mar 15 2005
prev sibling parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Tue, 15 Mar 2005 10:29:32 +0000 (UTC), AEon <AEon_member pathlink.com>  
wrote:
 Derek Parnell wrote:

 For example, you can also write ...
  writefln("Line: ", __LINE__);
 or
  writefln("Line: %d", __LINE__);
 to get the same results.

Indeed. Both work, you can even do this (as was pointed out, writefln is *very* flexible), though the formatting would be less than optimal: // Line: test.d 96Mar 15 200511:17:12Tue Mar 15 11:17:12 2005 writefln("Line: %d ", __FILE__,__LINE__,__DATE__,__TIME__,__TIMESTAMP__); // Line: test.d97Mar 15 200511:17:12Tue Mar 15 11:17:12 2005 writefln("Line: ", __FILE__,__LINE__,__DATE__,__TIME__,__TIMESTAMP__);

writefln accepts the same formatting as printf, eg. printf("Line: %s:%d %s %s %s\n",__FILE__,__LINE__,__DATE__,__TIME__,__TIMESTAMP__); writefln("Line: %s:%d %s %s %s",__FILE__,__LINE__,__DATE__,__TIME__,__TIMESTAMP__); should be identical. Or you could write it like: writefln("Line: ",__FILE__,":",__LINE__," ",__DATE__," ",__TIME__," ",__TIMESTAMP__); or even better (safer, no chance of stray %s causing problems) writefln("Line: %s",__FILE__,":%d",__LINE__," %s",__DATE__," %s",__TIME__," %s",__TIMESTAMP__); notice, writef<etc> can accept formatting strings anywhere.
 I did a full search in the dmd\ development tree for "__TIMESTAMP__",  
 other than
 in the dmd, and dmd.exe files, the only source related mention of the  
 latter is
 in

 E:\d\dmd\src\dmd\idgen.c

 struct Msgtable
 {
 char *ident;	// name to use in DMD source
 char *name;	// name in D executable
 };

 Msgtable msgtable[] =
 {
 { "LINE", "__LINE__" },
 { "FILE", "__FILE__" },
 { "DATE", "__DATE__" },
 { "TIME", "__TIME__" },
 { "TIMESTAMP", "__TIMESTAMP__" },
 ..
 }

 Would there have been any way to look up what type "__LINE__" is for  
 printf?

I notice Anders has given an example, which should work, but appears to highlight a bug in the compiler.
 I
 had assumed that simply all those __ vars are strings (as we found out D  
 Strings
 to be exact)?

I can see why you'd assume that. The only reason I knew __LINE__ was an 'int' is because it's an int in C/C++.
 PS: Any chance to fix this forum to actually respect indentations, to  
 make the reading of source samples easier to read?

I suspect not, because it has been voiced before and nothing has happened. The best you can do is prefix every line with a character i.e. # void main() { # int i; ..etc.. Regan
Mar 15 2005
next sibling parent Derek Parnell <derek psych.ward> writes:
On Wed, 16 Mar 2005 09:02:11 +1300, Regan Heath wrote:

[snip]
 
 I
 had assumed that simply all those __ vars are strings (as we found out D  
 Strings
 to be exact)?

I can see why you'd assume that. The only reason I knew __LINE__ was an 'int' is because it's an int in C/C++.

Also it is documented http://www.digitalmars.com/d/lex.html#specialtokens -- Derek Parnell Melbourne, Australia 16/03/2005 7:12:42 AM
Mar 15 2005
prev sibling parent AEon <AEon_member pathlink.com> writes:
Regan Heath wrote...

writefln("Line: %s",__FILE__,":%d",__LINE__," %s",__DATE__,"  
%s",__TIME__," %s",__TIMESTAMP__);

notice, writef<etc> can accept formatting strings anywhere.

Very interesting tip, I am starting to learn what amazing things you can *finally* do, but I would never have tried the above.
Also it is documented  http://www.digitalmars.com/d/lex.html#specialtokens

Sigh, I had read that but somehow I seem to have skipped the "__LINE__ integer literal" part every time. Thanx :) AEon
Mar 15 2005
prev sibling parent reply John Reimer <brk_6502 yahoo.com> writes:
Regan Heath wrote:
 On Tue, 15 Mar 2005 00:00:56 +0000 (UTC), AEon 
 <AEon_member pathlink.com>  wrote:
 
 In article <opsnngzocg23k2f5 nrage.netwin.co.nz>, Regan Heath says...

 // -> File:                Error: Access Violation
 printf("   File:        %s\n",__FILE__);


 I assume it's because __FILE__ is a D string, and not a 
 null-terminated  C
 string.

Right, I had no real idea what type __FILE__ was. Strangely: printf("Line: %.*s\n",__LINE__); yields: "Line: (null)", whereas: writefln("Line: %s",__LINE__); yields: "Line: 80", some valid number. Did I stumble on some known bug? Or am I missing some special case?

__LINE__ is not a char[] it is an integer type perhaps 'int' eg. printf("%d\n",__LINE__); I am not sure why: writefln("Line: %s",__LINE__); works, writefln must be doing something clever. Regan

Perhaps because: writefln("Line: ", __LINE__); works... writefln knows how to deal with the types of the passed args without specifying the string formatting (only if sequential printing or the args is desired, though). The %s is probably ignored in your sample because the type doesn't match? -JJR
Mar 14 2005
parent reply "Walter" <newshound digitalmars.com> writes:
"John Reimer" <brk_6502 yahoo.com> wrote in message
news:d15ahi$2u6f$1 digitaldaemon.com...
 The %s is probably ignored in your sample
 because the type doesn't match?

No, it isn't ignored. %s means "format the argument as a string, and insert it here." All argument types writefln knows about can be formatted as strings (after all, that's the whole point of it!). For example, writefln("hello %s baby", __LINE__); writes: hello 47 baby assuming it is on line 47. %d, %u, %x are there so one can format integral types into signed integers, unsigned integers, or hex strings. The format specifiers in printf specify both type and format. For writef, they only specify the format. The type is already known to writef. An article on this was in the January Dr. Dobb's.
Mar 14 2005
parent John Reimer <brk_6502 yahoo.com> writes:
Walter wrote:
 "John Reimer" <brk_6502 yahoo.com> wrote in message
 news:d15ahi$2u6f$1 digitaldaemon.com...
 
The %s is probably ignored in your sample
because the type doesn't match?

No, it isn't ignored. %s means "format the argument as a string, and insert it here." All argument types writefln knows about can be formatted as strings (after all, that's the whole point of it!). For example, writefln("hello %s baby", __LINE__); writes: hello 47 baby assuming it is on line 47. %d, %u, %x are there so one can format integral types into signed integers, unsigned integers, or hex strings. The format specifiers in printf specify both type and format. For writef, they only specify the format. The type is already known to writef. An article on this was in the January Dr. Dobb's.

Thanks for the clarification. That makes much more sense.
Mar 14 2005
prev sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
AEon wrote:

 How come printf does not take __FILE__ well?

You need to use either "%.*s" or toStringz() :
 printf("   File:		%.*s\n",__FILE__);

 printf("   File:		%s\n",std.string.toStringz(__FILE__));

Or:
 writefln("   File:      %s",__FILE__);

Either is fine, but writef is probably easiest ? Strings in D are not zero-terminated, unless they are string literals (""), and that was what bit you. --anders
Mar 14 2005
prev sibling parent Derek Parnell <derek psych.ward> writes:
On Mon, 14 Mar 2005 19:41:55 +0000 (UTC), AEon wrote:

 How does one make the below __VAR__ info usable in some way?
 
 ---------------------------
 import std.c.stdio;
 // import std.file;
 
 int main (char[][] args)
 {
 
 printf("   Testing the D Code\n");
 
 //	test.d(9): undefined identifier __FILE__
 //	make: *** [test.o] Error 1
 printf("   File:		%s\n",__FILE__);
 printf("   Line:		%s\n",__LINE__);
 printf("   Date:		%s\n",__DATE__);
 printf("   Time: 		%s\n",__TIME__);
 printf("   TimeStamp:	%s\n",__TIMESTAMP__);
 
 return 0;
 }
 ---------------------------
 
 Is there some place where I could look up such things. Where to ask "newbie"
 questions?
 
 I am aware of http://www.digitalmars.com/d/index.html, but the docs are at
times
 without examples.
 
 Presently I am testing many of the things in the docs, and spamming this forum
 is probable not the best idea?!
 
 AEon

than 'printf' ... <code> import std.stdio; int main (char[][] args) { writefln(" Testing the D Code\n"); writefln(" File: %s",__FILE__); writefln(" Line: %s",__LINE__); writefln(" Date: %s",__DATE__); writefln(" Time: %s",__TIME__); writefln(" TimeStamp: %s",__TIMESTAMP__); return 0; } </code> -- Derek Parnell Melbourne, Australia 15/03/2005 7:03:41 AM
Mar 14 2005