Thread overview
[Issue 4629] New: BufferedFile.printf() wants char[] as first argument
Aug 30, 2010
Andrej Mitrovic
Apr 21, 2012
SomeDude
August 12, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4629

           Summary: BufferedFile.printf() wants char[] as first argument
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-08-11 19:38:30 PDT ---
Using dmd 2.048 on this code:


import std.stream: BufferedFile, FileMode;
void main() {
    auto f = new BufferedFile("testfile.t", FileMode.Out);
    f.printf("%d\n", 10);
    f.close();
}


It shows the errors:
test.d(4): Error: function std.stream.Stream.printf (char[] format,...) is not
callable using argument types (string,int)
test.d(4): Error: cannot implicitly convert expression ("%d\x0a") of type
string to char[]


This gives no errors:
f.printf(cast(char[])"%d\n", 10);

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 30, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4629


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2010-08-29 18:42:59 PDT ---
Casting a string literal to a char[] is esentially undefined behavior. If you need a char[] out of a string literal, use .dup:

import std.stream: BufferedFile, FileMode;

void main()
{
    auto f = new BufferedFile("testfile.t", FileMode.Out);

    f.printf("%d\n".dup, 10);
    f.close();
}

I'm guessing printf takes a char[] due to it's C heritage? So far I've seen a lot of D1 code that uses char[] and when porting it to D2 one needs to either change all declarations/arguments to use a string, or at the very least use .dup when passing string literals to functions taking char[].


(In reply to comment #0)
> Using dmd 2.048 on this code:
> 
> 
> import std.stream: BufferedFile, FileMode;
> void main() {
>     auto f = new BufferedFile("testfile.t", FileMode.Out);
>     f.printf("%d\n", 10);
>     f.close();
> }
> 
> 
> It shows the errors:
> test.d(4): Error: function std.stream.Stream.printf (char[] format,...) is not
> callable using argument types (string,int)
> test.d(4): Error: cannot implicitly convert expression ("%d\x0a") of type
> string to char[]
> 
> 
> This gives no errors:
> f.printf(cast(char[])"%d\n", 10);

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 09, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4629


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei@metalanguage.com
         AssignedTo|nobody@puremagic.com        |andrei@metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4629


SomeDude <lovelydear@mailmetrash.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lovelydear@mailmetrash.com


--- Comment #2 from SomeDude <lovelydear@mailmetrash.com> 2012-04-21 15:19:12 PDT ---
Compiles on 2.059

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 25, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4629


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------