Thread overview | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 29, 2005 Using writef to print strings that contain format specifiers | ||||
---|---|---|---|---|
| ||||
While printing file names on my system using writef, I came across this file name: 311abc%20-%20tmb[1] (percent signs included) and received this error: 311abcError: std.format. I'm guessing writef got tripped up because the % sign is used as part of a format string for writef. I tried taking the file name and putting single, double and the wysiwyg identifier "r" in front of it and tried to print it with writef, but I still get the error. Is there any way around this? I wouldn't have been able to anticipate thie file name on my system, so, maybe writef is not a good candidate for what I'm doing? This will reproduce the error: # import std.stdio; # # void main() # { # writef("311abc%20-%20tmb[1]"); # } -Kramer |
January 30, 2005 Re: Using writef to print strings that contain format specifiers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kramer | Kramer wrote: > I'm guessing writef got tripped up because the % sign is used as part of a > format string for writef. I tried taking the file name and putting single, > double and the wysiwyg identifier "r" in front of it and tried to print it with > writef, but I still get the error. Is there any way around this? I wouldn't > have been able to anticipate thie file name on my system, so, maybe writef is > not a good candidate for what I'm doing? The first argument to writef is *not* a string, but a format specifier. Thus, you need to use this instead: writefln("%s","311abc%20-%20tmb[1]") Works the same way as with printf (in C). --anders |
January 30, 2005 Re: Using writef to print strings that contain format specifiers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kramer | In article <cth792$mc8$1@digitaldaemon.com>, Kramer says... > >Is there any way around this? >This will reproduce the error: ># import std.stdio; ># ># void main() ># { ># writef("311abc%20-%20tmb[1]"); ># } Try this instead: > writefln("%s","311abc%20-%20tmb[1]"); - EricAnderton at yahoo |
January 30, 2005 Re: Using writef to print strings that contain format specifiers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | D'oh! Thanks! -Kramer In article <cth8jr$ni2$1@digitaldaemon.com>, =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says... > >Kramer wrote: > >> I'm guessing writef got tripped up because the % sign is used as part of a format string for writef. I tried taking the file name and putting single, double and the wysiwyg identifier "r" in front of it and tried to print it with writef, but I still get the error. Is there any way around this? I wouldn't have been able to anticipate thie file name on my system, so, maybe writef is not a good candidate for what I'm doing? > >The first argument to writef is *not* a string, but a format specifier. > >Thus, you need to use this instead: writefln("%s","311abc%20-%20tmb[1]") > >Works the same way as with printf (in C). > >--anders |
January 31, 2005 Re: Using writef to print strings that contain format specifiers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | In article <cth8jr$ni2$1@digitaldaemon.com>, =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says... > >The first argument to writef is *not* a string, but a format specifier. > >Thus, you need to use this instead: writefln("%s","311abc%20-%20tmb[1]") > >Works the same way as with printf (in C). > >--anders This seems to be a common source of mistakes, and maybe something should be done about it. We could for example have two additional functions called write()/writeln(), which do not accept format strings, but otherwise works as writef() and writefln()? Nick |
January 31, 2005 Re: Using writef to print strings that contain format specifiers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick | Nick wrote:
>>The first argument to writef is *not* a string, but a format specifier.
>>Thus, you need to use this instead: writefln("%s","311abc%20-%20tmb[1]")
>>
>>Works the same way as with printf (in C).
>
> This seems to be a common source of mistakes, and maybe something should be done
> about it. We could for example have two additional functions called
> write()/writeln(), which do not accept format strings, but otherwise works as
> writef() and writefln()?
Yes, seems like a good idea. In C, there is "puts" which does writeln.
Such a "stupid" function should also be faster than the full writef ?
I believe OutputStream has a bunch of overloaded "write()" functions,
but there are no such alternatives in std.stdio as far as I can tell.
I'm also wondering how you output an ubyte[] full of 8-bit characters ?
Using "%s" just gives the "invalid UTF-8 sequence" error with writef...
Input and output to non-Unicode/UTF is something of a pain, in general.
But I suppose I can do it on the stdin/stdout streams instead of stdio.
--anders
|
January 31, 2005 Re: Using writef to print strings that contain format specifiers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick | Nick wrote: > This seems to be a common source of mistakes, and maybe something should be done > about it. We could for example have two additional functions called > write()/writeln(), which do not accept format strings, but otherwise works as > writef() and writefln()? Here's one trivial implementation of those: > import std.stdio; > import std.stream; > > void write(char[] str) > { > std.stream.stdout.writeString(str); > } > > void writeln(char[] str) > { > std.stream.stdout.writeLine(str); > } > > void main() > { > writef("*** Hall\u00e5, "); > writefln("V\u00e4rlden! ***"); > write("%%% Hall\u00e5, "); > writeln("V\u00e4rlden! %%%"); > } It doesn't work for wchar[] or dchar[], though ? (You can't overload them, because of the literals) --anders |
January 31, 2005 Re: Using writef to print strings that contain format specifiers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | In article <ctlfea$nh7$1@digitaldaemon.com>, =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says... > >Nick wrote: > >> This seems to be a common source of mistakes, and maybe something should be done >> about it. We could for example have two additional functions called >> write()/writeln(), which do not accept format strings, but otherwise works as >> writef() and writefln()? > >Here's one trivial implementation of those: > > [snip] I was thinking more in the line of functions which can print anything, not just strings. So writing eg. # int i, j; # char[] str; # ... # writeln(i, str, j, " hello"); would do exactly the same as # writefln(i, str, j, " hello"); except it would be safe if str contains any '%'-characters. Nick |
January 31, 2005 Re: Using writef to print strings that contain format specifiers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick | Nick schrieb:
> I was thinking more in the line of functions which can print anything, not just
> strings. So writing eg.
>
> # int i, j;
> # char[] str;
> # ...
> # writeln(i, str, j, " hello");
>
> would do exactly the same as
>
> # writefln(i, str, j, " hello");
>
> except it would be safe if str contains any '%'-characters.
This would mean adding another way to accomplish exactly the same as writef does, which in general is a bad idea.
It isn't easier to explain to a noob the difference between write/writef than between writef(str) and writef("%s", str).
-Sebastian
|
January 31, 2005 Re: Using writef to print strings that contain format specifiers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sebastian Beschke | In article <ctlqte$17hd$1@digitaldaemon.com>, Sebastian Beschke says... > >This would mean adding another way to accomplish exactly the same as writef does, which in general is a bad idea. Why is that a bad idea? (I'm not contesting that it is, just asking.) >It isn't easier to explain to a noob the difference between write/writef than between writef(str) and writef("%s", str). Sure it is. You say "Use write/writeln for output, eg. writeln(a,b,c). If you need specially formatted output, use the writef/writefln, eg. writefln("%-20s %5d", s, i);" But saying that you can't write eg. int i; cdouble d; MyNumberClass m; .. writefln(i, d, m); because m.toString might in theory return a '%' seems pretty counter-intuitive too me. Nick |
Copyright © 1999-2021 by the D Language Foundation