www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - refuses to open file

reply rbtwms paradiseca.com writes:
I wrote a program and had it working fine,  Then made a change so I could
change the file name from the command line.  Now the progrram refuses to
open the file.
File efile;
  writefln("command line %s", args[]);

    if(args.length == 3) {
    	type = to!int( args[1] );
//			writefln("args[2] is: %s",args[2]);
      }
    if (args.length == 2){
        efile = File(args[2]);
        }
    if (args.length == 1){
      efile = File("key"); // I moved file to dir the program is in
     }
orignal just had a line  efile = File("l:\\mvammach\\key");
command line ["eqipfile"]
std.exception.ErrnoException std\stdio.d(289): Cannot open file ` KEY ' in
mode `rb' (No such file or directory)
----------------
0x0041B4B8
0x0041B343
0x00411E57
0x0040BB38
0x0040BB72
0x0040B794
0x00422B2D
0x750233AA in BaseThreadInitThunk
0x77369EF2 in RtlInitializeExceptionChain
0x77369EC5 in RtlInitializeExceptionChain
----------------
 Volume in drive C is OS
 Volume Serial Number is E221-AB60

 Directory of C:\D\projects\equipment

04/16/2013  04:41 PM    <DIR>          .
04/16/2013  04:41 PM    <DIR>          ..
04/13/2013  06:39 PM            17,764 eqip.txt
04/14/2013  03:26 PM             1,650 eqipfile.bak
04/16/2013  04:44 PM             2,469 eqipfile.d
04/16/2013  04:40 PM           265,244 eqipfile.exe
04/16/2013  04:40 PM           120,600 eqipfile.obj
04/16/2013  04:41 PM               369 error.txt
01/04/2013  10:04 AM         1,909,000 KEY
04/16/2013  04:22 PM    <DIR>          mvammach
               7 File(s)      2,317,096 bytes
               3 Dir(s)  896,994,713,600 bytes free
can anybody help with this?   	
Apr 16 2013
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 17 April 2013 at 01:41:58 UTC, 
rbtwms paradiseca.com wrote:

 File efile;
 writefln("command line %s", args[]);

     if(args.length == 3) {
         type = to!int( args[1] );
 //      writefln("args[2] is: %s",args[2]);
     }
     if (args.length == 2) {
         efile = File(args[2]);
     }
     if (args.length == 1) {
         efile = File("key"); // I moved file to dir the program 
 is in
     }
 orignal just had a line  efile = File("l:\\mvammach\\key");
 command line ["eqipfile"]
 std.exception.ErrnoException std\stdio.d(289): Cannot open file 
 ` KEY ' in
 mode `rb' (No such file or directory)
A couple of things:
     if (args.length == 2) {
         efile = File(args[2]);
     }
If args.length == 2 then args[2] doesn't exist.
 command line ["eqipfile"]
I don't understand where this is from...
Apr 17 2013
parent reply rbtwms digitalpath.net writes:
I had made so many changes truing to solve the problem that I included a
older version. another problem is that with the file name: efile =
File("l:\\filepro\\mvmammach\\key") with no space before the drive letter
the silently quits with no error but as you can see efile name is just a hex
number.(see (1))  putting a space before the drive letter causes the
exception (see (2)).


(1)
command line ["eqipfile"]
defile name is File(2002FE0)
WITHOUT SPACE!

(2)
command line ["eqipfile"]
std.exception.ErrnoException std\stdio.d(289): Cannot open file `
l:\filepro\mvammach\key ' in mode `rb' (Invalid argument)
----------------
0x0041B478
0x0041B303
0x004101E3
0x0040BB04
0x0040BB3E
0x0040B760
0x00422AED
0x770133AA in BaseThreadInitThunk
0x77DB9EF2 in RtlInitializeExceptionChain
0x77DB9EC5 in RtlInitializeExceptionChain


WITH SPACE!

code excerpt follows:

writefln("command line %s", args[]);
  efile = File(" l:\\filepro\\mvammach\\key");
writefln("efile name is %s", efile);  	    	
  char[4600] buf;
  string[int] list;
	efile.rawRead(buf);
  while(efile.rawRead(buf)){
    if(buf[0] != 0x01) continue;
    string c = to!string (buf[107..111]);
    if (c == "    ") break;
    int z = to!int(c);
    string s = to!string(buf[67..92]);
    list[z] = s;
    }
Sorry I seen to have had my identity set up wrong it is corrected now.
Apr 17 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/17/2013 02:57 PM, rbtwms digitalpath.net wrote:

 I had made so many changes truing to solve the problem that I included a
 older version. another problem is that with the file name: efile =
 File("l:\\filepro\\mvmammach\\key") with no space before the drive letter
 the silently quits with no error but as you can see efile name is 
just a hex
 number.(see (1))  putting a space before the drive letter causes the
 exception (see (2)).


 (1)
 command line ["eqipfile"]
 defile name is File(2002FE0)
Is it really "defile" that gets printed up there, or is that another typo? I highly recommend that you provide a complete program for us to try.
 WITHOUT SPACE!

 (2)
 command line ["eqipfile"]
 std.exception.ErrnoException std\stdio.d(289): Cannot open file `
 l:\filepro\mvammach\key ' in mode `rb' (Invalid argument)
This time it seems to be complaining about a space *after* the file name! (?)
 ----------------
 0x0041B478
 0x0041B303
 0x004101E3
 0x0040BB04
 0x0040BB3E
 0x0040B760
 0x00422AED
 0x770133AA in BaseThreadInitThunk
 0x77DB9EF2 in RtlInitializeExceptionChain
 0x77DB9EC5 in RtlInitializeExceptionChain


 WITH SPACE!

 code excerpt follows:

 writefln("command line %s", args[]);
    efile = File(" l:\\filepro\\mvammach\\key");
I have a vague recollection that forward slashes work with file names even under Windows.
 writefln("efile name is %s", efile);  	    	
    char[4600] buf;
That doesn't look right: char is a UTF-8 code unit. Unless you are sure that the file contains UTF-8, I recommend you use something else, e.g. ubyte.
    string[int] list;
 	efile.rawRead(buf);
Do you intend to ignore that read? The data will be overwritten by the following rawRead.
    while(efile.rawRead(buf)){
      if(buf[0] != 0x01) continue;
      string c = to!string (buf[107..111]);
You are doing that because you are sure that there is a valid Unicode character there? (Although I am not sure how it is related to the problem.)
      if (c == "    ") break;
      int z = to!int(c);
      string s = to!string(buf[67..92]);
'to' may throw an exception. Try wrapping that section in a try block to see whether you get any information from that exception. Also, try compiling with -g or -gc to see whether the backtrace will be more useful.
      list[z] = s;
      }
 Sorry I seen to have had my identity set up wrong it is corrected now.
Ali
Apr 17 2013
parent reply rbtwms digitalpath.net writes:
Is it really "defile" that gets printed up there, or is that another
typo? I highly recommend that you provide a complete program for us to try.
spell checker got away from me.

This time it seems to be complaining about a space *after* the file
name! (?)
makes no difference.

I have a vague recollection that forward slashes work with file names
even under Windows.
makes no deference.

That doesn't look right: char is a UTF-8 code unit. Unless you are sure
that the file contains UTF-8, I recommend you use something else, e.g.
ubyte.
It' a pure ascii file.

Do you intend to ignore that read? The data will be overwritten by the
following rawRead.
Yes it is a leader for the data file, all records same length.

You are doing that because you are sure that there is a valid Unicode
character there? (Although I am not sure how it is related to the problem.)
Yes each record that has data in it starts with hex01 the first 20 bytes is
non ascii junk.

The thing is the program was working until I tried to add the command line
args.
The data file is quite large 01/04/2013  10:04 AM         1,909,000 KEY

complete source follows:

// Equipment file display
import std.stdio, std.string, std.conv, std.algorithm;
struct equipment {
  char[20] header;
  char[8] sysnum;
  char[2] type;
  char[5] eznum;
  char[12] manfac;
  char[3] ver;
  char[10] mod;
  char[2] junk1; // info we dont need
  char[5] price; // price per play
  char[25] name;
  char[15] sernun;
  char[10] opnum;
  char[40] junk2; // more info we dont need
  char[25] purchfrom;
  char[10] purchdate;
  char[11] cost;
  char status;
  char[4] locnum;
  char[4] junk3;
  char[30] locname;
  char[382] junk4;
  char[15] loccity;
  char[2] state;
  char[5220] junk5;
  }


void main (string[] args)
{
	
  File efile;
  equipment eqip;
  int type;
  efile = File("l:\\filepro\\mvmmach\\key");
/*
writeln("args.length = ", args.length);
	switch (args.length){
  	case 1:
    	efile = File ((to!string(" l:\\filepro\\mvammach\\key ")));
		  writefln("efile name is %s", efile);	
    	break;
    case 2:
      efile = File(args[1]);
      writefln("efile name is %s", efile);
      break;
    case 3:
    	type = to!int( args[2] );
      break;
    default:
      break;   	
     }

writefln("efile name is %s", efile);  	    	
  char[4600] buf;
  string[int] list;
	efile.rawRead(buf);
  while(efile.rawRead(buf)){
    if(buf[0] != 0x01) continue;
    string c = to!string (buf[107..111]);
    if (c == "    ") break;
    int z = to!int(c);
    string s = to!string(buf[67..92]);
    list[z] = s;
    }
  int numb[] = list.keys;
  sort (numb); 		//	{ return list[a] < list[b]; })(numb);
  	foreach(num; numb){
//    writef("num=[%d],type[%d]",num,type);
      if((num < type+999) && ( num > type)){
        writefln("Opnum = %s Name = %s", num, list[num] );
        }
      }
  }
Apr 17 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/17/2013 04:48 PM, rbtwms digitalpath.net wrote:

I said:

 That doesn't look right: char is a UTF-8 code unit. Unless you are sure
 that the file contains UTF-8, I recommend you use something else, e.g.
 ubyte.
You said:
 It' a pure ascii file.
That means that every byte value in the file is in the range [0, 127], right? ASCII...
      string c = to!string (buf[107..111]);
For that to work, every one of those bytes must be in the range [0, 127].
      if (c == "    ") break;
      int z = to!int(c);
The fact that you are trying to convert those four bytes to a 32 value makes me think that the file is not "pure ascii" after all. Unless the int value written in that location of the file has the property that none of the four 8 bits of it are above 127. However, I don't understand whether your actual problem is related to that. Ali
Apr 17 2013
parent reply rbtwms digitalpath.net writes:
Let's start over it has nothing to due with the file.  It's the file name
thats the problem.  I wote a short test program to demenstrate the problem.
It shows that if the file name has a path it causes the exception. i.e:

(prompt)dir
 Volume in drive C is OS
 Volume Serial Number is E221-AB60

 Directory of c:\d\projects\equipment

04/18/2013  03:57 PM             1,070 test.d
               1 File(s)          1,070 bytes
               0 Dir(s)  899,752,787,968 bytes free
(prompt)test 2000 c:\d\projects\equipment\test.d
args.length = 3
out of switch
Type is 2000 case 2
std.exception.ErrnoException std\stdio.d(289): Cannot open file
`c:\d\projects\eqipment\test.d' in mode `rb' (No such file or directory)
----------------
0x0041A83C
0x0041A6C7
0x0040F577
0x0040B240
0x0040B27A
0x0040AE9C
0x00421F3D
0x752E33AA in BaseThreadInitThunk
0x770C9EF2 in RtlInitializeExceptionChain
0x770C9EC5 in RtlInitializeExceptionChain
----------------
(prompt)test 2000 test.d
out of switch
args.length = 3
out of switch
Type is 2000 case 2
out of switch
args[2] = [test.d]
efile name is File(1F81FE0) case 3
out of switch
out of for
Command line was: ["test", "2000", "test.d"]
File is open

test.d follows:
/* Test program
To test file opening problem
*/
import std.stdio, std.string, std.conv;
void main (string[] args)
{
	auto fname = "l:\\filepro\\mvammach\\key";
  File efile;
  int type;

writeln("args.length = ", args.length);
	if (args.length >= 3){
  	for (int n=1; n <= args.length; n++){
      switch (n){
        case 1:
          break;
        case 2:
          type = to!int( args[1] );
          writefln("Type is %s case 2", type);
          break;
        case 3:
          efile = File(args[2]);
          writefln("args[2] = [%s]", args[2]);
          writefln("efile name is %s case 3", efile);
          break;
        default:
          break;   	
         } // end switch
         writeln("out of switch");
	   } // end for
     writeln("out of for");
   } // end if
   else {
     efile = File(fname,"r");
     writefln("fname = [%s] in else", fname);
		 writefln("efile = [%s]", efile);
   } // end else
writeln("Command line was: ", args[]);
if(efile.isOpen()) writeln ("File is open");
} // end main
Apr 18 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/18/2013 04:25 PM, rbtwms digitalpath.net wrote:

 Let's start over it has nothing to due with the file.  It's the file name
 thats the problem.
Let's start with the obvious. :)
   Directory of c:\d\projects\equipment
 std.exception.ErrnoException std\stdio.d(289): Cannot open file
 `c:\d\projects\eqipment\test.d' in mode `rb' (No such file or directory)
There is a typo in that path. Ali
Apr 18 2013
parent rbtwms digitalpath.net writes:
Thanks so much for your kind assistance.  The problem was really my own
stupidity and just started learning D.
The exception was cause by the space in the filename.
Thanks again for your help.
Bob
Apr 19 2013