error with reading file name

Ali Çehreli acehreli at yahoo.com
Thu Dec 6 09:31:46 PST 2012


On 12/06/2012 07:52 AM, Suliman wrote:
> I am trying to create simple app that would read user input and open
> file with such name, but every time when I run it's crash with error
>
> "std.file.FileException at std\file.d(294): \1.txt"
>
> import std.stdio;
> import std.string;
> import std.file;
>
> void main()
> {
> string getfilename()
> {
> auto name = readln();
> writeln(name);
> if (exists(name))
> {
> writeln("exist");
> }
> else
> writeln("not exist");
>
> return name;
> }
>
> void readfile(string name)
> {
> auto filearray = read(name);
> writeln(name);
> writeln(filearray);
> }
> readfile(getfilename());
>
>
> }

The exception is thrown here:

     version(Windows)
     {
         alias TypeTuple!(GENERIC_READ,
                 FILE_SHARE_READ, (SECURITY_ATTRIBUTES*).init, 
OPEN_EXISTING,
                 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
                 HANDLE.init)
             defaults;
         auto h = CreateFileW(std.utf.toUTF16z(name), defaults);

         cenforce(h != INVALID_HANDLE_VALUE, name);    // <-- HERE

It seems to be related to a Unicode encoding issue, possibly the 
encoding that the console is using. (I am assuming that getfilename() 
reads from the console.)

It could be about file access rights as well.

Ali



More information about the Digitalmars-d-learn mailing list