Strange behavior of read file

monarch_dodra monarchdodra at gmail.com
Tue Aug 27 10:18:03 PDT 2013


On Tuesday, 27 August 2013 at 15:45:06 UTC, Paul Jurczak wrote:
> module main;
>
> import std.stdio, std.file, std.string, std.algorithm, 
> std.range, std.datetime, std.conv, std.typetuple;
>
> int f(string fileName = r"C:\Euler\data\e67.txt") {
>    auto text = read(fileName);
>    return text.length;
> }
>
> void main()
> {
>   try {
>       string fileName = r"C:\Euler\data\e67.txt";
>       auto text = read(fileName);
>       writeln(text.length);
>
>       writeln(f);  // **** EXCEPTION HERE
>    }
>    catch (Exception e) {
>       writeln(e);
>    }
>
>    writeln(f);
> }
>
> I'm running this test code compiled with DMD 2.063.2 on Windows 
> 7 and I'm getting an exception "The system cannot find the file 
> specified". The same code runs fine on Linux. The file in 
> question exists of course.
>
> When I remove the default parameter value from f(), exceptions 
> stop. Is there a problem of having two strings 
> r"C:\Euler\data\e67.txt" with the same content?

Works for me on my own win7 with 2.063.2 (replaced file with
r"C:\D\test.txt"). Not sure what else is going on. Did you copy
paste the *exact* code that was failing? Please try inserting
this trace:

int f(string fileName = r"C:\D\test.txt") {
     writeln("fileName: ", fileName); //HERE
     auto text = read(fileName);
     return text.length;
}

And report what happens.

Off topic, "text" might be a poor choice for a variable name, as
it shadows the pretty common function "std.conv.text"


More information about the Digitalmars-d-learn mailing list