www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - readText fails to open file

reply "GreatEmerald" <pastas4 gmail.com> writes:
This is kind of silly, and I probably missed something, but for 
some reason I can't get any kind of text file opened when using 
readText from std.file. This is what I'm trying to do:

   import std.stdio;
   import std.file;

   int main(string[] args)
   {
       if (!isFile(args[0]))
       {
           writeln("Error: Input file does not exist or is not a 
valid file!");
           return 1;
       }

       auto LTFFile = chomp(readText(args[0]));

       readln();
       return 0;
   }

Nice and simple, right? So I execute it:

   > ./LTF2LIP LTF2LIP.d
   std.utf.UTFException std/utf.d(645): Invalid UTF-8 sequence (at 
index 1)

And I'm sure that the file is in UTF-8, with LF line endings, 
without a BOM. The same error is thrown when I try any other kind 
of files. So what gives?..
Jun 17 2012
next sibling parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 17.06.2012 12:21, GreatEmerald wrote:
 This is kind of silly, and I probably missed something, but for some
 reason I can't get any kind of text file opened when using readText from
 std.file. This is what I'm trying to do:

 import std.stdio;
 import std.file;

 int main(string[] args)
 {
 if (!isFile(args[0]))
 {
 writeln("Error: Input file does not exist or is not a valid file!");
 return 1;
 }

 auto LTFFile = chomp(readText(args[0]));

 readln();
 return 0;
 }

 Nice and simple, right? So I execute it:

  > ./LTF2LIP LTF2LIP.d
 std.utf.UTFException std/utf.d(645): Invalid UTF-8 sequence (at index 1)
It's a BOM most likely. There has been talk on making readText work with them. I'm not sure how you'd check there is no BOM, most text editors will hide it. Hex editor may help though.
 And I'm sure that the file is in UTF-8, with LF line endings, without a
 BOM. The same error is thrown when I try any other kind of files. So
 what gives?..
Other then this try cast(string)read(args[0]); it's not any worse. -- Dmitry Olshansky
Jun 17 2012
parent "GreatEmerald" <pastas4 gmail.com> writes:
Oh, I just figured out what was going wrong. Apparently, args[0] 
is the path to the program itself, and not the first argument. 
args[1] is what I need to start reading from!
Jun 17 2012
prev sibling next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/17/2012 01:21 AM, GreatEmerald wrote:
 This is kind of silly, and I probably missed something, but for some
 reason I can't get any kind of text file opened when using readText from
 std.file. This is what I'm trying to do:

 import std.stdio;
 import std.file;

 int main(string[] args)
 {
 if (!isFile(args[0]))
 {
 writeln("Error: Input file does not exist or is not a valid file!");
 return 1;
 }

 auto LTFFile = chomp(readText(args[0]));

 readln();
 return 0;
 }

 Nice and simple, right? So I execute it:

  > ./LTF2LIP LTF2LIP.d
 std.utf.UTFException std/utf.d(645): Invalid UTF-8 sequence (at index 1)

 And I'm sure that the file is in UTF-8, with LF line endings, without a
 BOM. The same error is thrown when I try any other kind of files. So
 what gives?..
Try args[1]. Your compiled program is surely not UTF-8. ;) -- D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
Jun 17 2012
prev sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, June 17, 2012 10:21:17 GreatEmerald wrote:
 This is kind of silly, and I probably missed something, but for
 some reason I can't get any kind of text file opened when using
 readText from std.file. This is what I'm trying to do:
 
    import std.stdio;
    import std.file;
 
    int main(string[] args)
    {
        if (!isFile(args[0]))
        {
            writeln("Error: Input file does not exist or is not a
 valid file!");
            return 1;
        }
 
        auto LTFFile = chomp(readText(args[0]));
 
        readln();
        return 0;
    }
 
 Nice and simple, right? So I execute it:
    > ./LTF2LIP LTF2LIP.d
 
    std.utf.UTFException std/utf.d(645): Invalid UTF-8 sequence (at
 index 1)
 
 And I'm sure that the file is in UTF-8, with LF line endings,
 without a BOM. The same error is thrown when I try any other kind
 of files. So what gives?..
LOL. You're reading the wrong file. You have made the common mistake of thinking that args[0] was the first argument that you passed to your program. It's not. It's the name of your program. In this case, it would be "./LTF2LIP". It's args[1] which is "LTF2LIP.d". Also, you need to check exists before you check isFile, otherwise isFile will blow up if the file doesn't exst. And both of those are properties, so you should be calling them like auto filename = args[1]; if(!filename.exists || !filename.isFile) ... The -property flag enforces this behavior, and it will eventually become the normal compiler behavior. - Jonathan M Davis
Jun 17 2012
parent "GreatEmerald" <pastas4 gmail.com> writes:
On Sunday, 17 June 2012 at 08:35:58 UTC, Jonathan M Davis wrote:
 Also, you need to check exists before you check isFile, 
 otherwise isFile will
 blow up if the file doesn't exst. And both of those are 
 properties, so you
 should be calling them like

 auto filename = args[1];

 if(!filename.exists || !filename.isFile)
     ...

 The -property flag enforces this behavior, and it will 
 eventually become the
 normal compiler behavior.

 - Jonathan M Davis
Oh, I see, thanks!
Jun 17 2012