www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using writefln with format specifiers in string

reply Kramer <Kramer_member pathlink.com> writes:
I'm hoping someone has run across this before...

I was listing some files in a directory using listdir and then printing out the
names using writefln, but the program kept terminating before finishing printing
all the contents.  I finally figured out it was because in one of the filenames
there was a percent sign.

So, running the following program on DMD 0.146 will produce a FormatError:







How can I get around this?

I'm running Windows XP SP2 and I tried the command chcp 65001 to set my console
to UTF-8 thinking that might be it, but no go.  Then I piped this to a file and
lo and behold, the output stopped in the file at the same point on the console.

Any help on this would be much appreciated?

-Kramer
Feb 13 2006
parent reply "Derek Parnell" <derek psych.ward> writes:
On Tue, 14 Feb 2006 18:00:37 +1100, Kramer <Kramer_member pathlink.com>  
wrote:


 How can I get around this?
writefln("%s", FileName); -- Derek Parnell Melbourne, Australia
Feb 13 2006
parent reply Kramer <Kramer_member pathlink.com> writes:
In article <op.s4yb3wev6b8z09 ginger.vic.bigpond.net.au>, Derek Parnell says...
On Tue, 14 Feb 2006 18:00:37 +1100, Kramer <Kramer_member pathlink.com>  
wrote:


 How can I get around this?
writefln("%s", FileName); -- Derek Parnell Melbourne, Australia
Ugg, thanks Derek. Reading the docs help, but reading them carefully helps more. <g> -Kramer
Feb 14 2006
parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Kramer wrote:
 In article <op.s4yb3wev6b8z09 ginger.vic.bigpond.net.au>, Derek Parnell says...
 
On Tue, 14 Feb 2006 18:00:37 +1100, Kramer <Kramer_member pathlink.com>  
wrote:



How can I get around this?
writefln("%s", FileName); -- Derek Parnell Melbourne, Australia
Ugg, thanks Derek. Reading the docs help, but reading them carefully helps more. <g> -Kramer
I ran into this before :) I think there really should be a _standard_ writeraw() or just write() (with no f) which does exactly that .. void writer( char[] x ) { writef("%s", x); }
Feb 15 2006
next sibling parent reply "Chris Miller" <chris dprogramming.com> writes:
On Wed, 15 Feb 2006 09:59:48 -0500, Hasan Aljudy <hasan.aljudy gmail.com>  
wrote:

 Kramer wrote:
 In article <op.s4yb3wev6b8z09 ginger.vic.bigpond.net.au>, Derek Parnell  
 says...

 On Tue, 14 Feb 2006 18:00:37 +1100, Kramer  
 <Kramer_member pathlink.com>  wrote:



 How can I get around this?
writefln("%s", FileName); -- Derek Parnell Melbourne, Australia
Ugg, thanks Derek. Reading the docs help, but reading them carefully helps more. <g> -Kramer
I ran into this before :) I think there really should be a _standard_ writeraw() or just write() (with no f) which does exactly that .. void writer( char[] x ) { writef("%s", x); }
Yep, and writeln, but you could just use dout.writeString() / dout.writeLine().
Feb 15 2006
parent reply David Medlock <noone nowhere.com> writes:
Chris Miller wrote:
 On Wed, 15 Feb 2006 09:59:48 -0500, Hasan Aljudy 
 <hasan.aljudy gmail.com>  wrote:
 
 Kramer wrote:

 In article <op.s4yb3wev6b8z09 ginger.vic.bigpond.net.au>, Derek 
 Parnell  says...

 On Tue, 14 Feb 2006 18:00:37 +1100, Kramer  
 <Kramer_member pathlink.com>  wrote:



 How can I get around this?
writefln("%s", FileName); -- Derek Parnell Melbourne, Australia
Ugg, thanks Derek. Reading the docs help, but reading them carefully helps more. <g> -Kramer
I ran into this before :) I think there really should be a _standard_ writeraw() or just write() (with no f) which does exactly that .. void writer( char[] x ) { writef("%s", x); }
Yep, and writeln, but you could just use dout.writeString() / dout.writeLine().
Perhaps the formatting should allow '%%' to represent a single percent sign? -DavidM
Feb 15 2006
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"David Medlock" <noone nowhere.com> wrote in message 
news:dsvouc$1fl5$1 digitaldaemon.com...
 Perhaps the formatting should allow '%%' to represent a single percent 
 sign?
It does. writefln("5%%") outputs "5%." The problem is that he wants to print out arbitrary strings which may have single percent signs, and he doesn't want format() interpreting the percent signs as format specifiers.
Feb 15 2006
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Wed, 15 Feb 2006 07:59:48 -0700, Hasan Aljudy wrote:

 Kramer wrote:
 In article <op.s4yb3wev6b8z09 ginger.vic.bigpond.net.au>, Derek Parnell says...
 
On Tue, 14 Feb 2006 18:00:37 +1100, Kramer <Kramer_member pathlink.com>  
wrote:



How can I get around this?
writefln("%s", FileName); -- Derek Parnell Melbourne, Australia
Ugg, thanks Derek. Reading the docs help, but reading them carefully helps more. <g> -Kramer
I ran into this before :) I think there really should be a _standard_ writeraw() or just write() (with no f) which does exactly that .. void writer( char[] x ) { writef("%s", x); }
You could always just write such a function and send it to Walter. It might even make it to the official library. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 16/02/2006 10:12:47 AM
Feb 15 2006
next sibling parent Bruno Medeiros <daiphoenixNO SPAMlycos.com> writes:
Derek Parnell wrote:
 On Wed, 15 Feb 2006 07:59:48 -0700, Hasan Aljudy wrote:
 
 Kramer wrote:
 In article <op.s4yb3wev6b8z09 ginger.vic.bigpond.net.au>, Derek Parnell says...

 On Tue, 14 Feb 2006 18:00:37 +1100, Kramer <Kramer_member pathlink.com>  
 wrote:



 How can I get around this?
writefln("%s", FileName); -- Derek Parnell Melbourne, Australia
Ugg, thanks Derek. Reading the docs help, but reading them carefully helps more. <g> -Kramer
I ran into this before :) I think there really should be a _standard_ writeraw() or just write() (with no f) which does exactly that .. void writer( char[] x ) { writef("%s", x); }
You could always just write such a function and send it to Walter. It might even make it to the official library.
Yup, I think we should have one such function (write and writeln) in the stdlib. -- Bruno Medeiros - CS/E student "Certain aspects of D are a pathway to many abilities some consider to be... unnatural."
Feb 16 2006
prev sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Derek Parnell wrote:

I think there really should be a _standard_ writeraw() or just write() 
(with no f) which does exactly that ..
You could always just write such a function and send it to Walter. It might even make it to the official library.
I wrote write, writeln, read, readln but there was no response. (i.e. they didn't make into std.stdio, but to din/dout instead) Will try again. Some day. --anders PS. http://www.digitalmars.com/d/archives/digitalmars/D/15627.html
Feb 18 2006