File not open for write


I am stuck at these "File not open for write" and "Bad file handle" errors. I check the lines which initialize the output file an open file:

opath = parapath $+ "\\mhrun" $+ mhrun;
opara = opath $+ "\\" $+ lmodel $+ lprior $+ "0" $+ subTstr $+ "pa0";
create fhpara=^opara with PARAM, npara, 8;

mhrun = "0";
lpath = parapath $+ "\\mhrun" $+ mhrun;

lpara = lpath $+ "\\" $+ lmodel $+ lprior $+ dataselstr $+ subTstr $+ "pa0";
open fhlpara = ^lpara for read;

opath = lpath;
opara = opath $+ "\\" $+ lmodel $+ lprior $+ dataselstr $+ subTstr $+ "pa";
create fhopara = ^opara with PARAM, npara, 8;

opath = resupath $+ "\\mhrun" $+ mhrun;
odet = opath $+ "\\" $+ lmodel $+ lprior $+ dataselstr $+ subTstr $+ "dt";
create fhdet = ^odet with DET, 2, 8;

How to understand the meaning of  structure ...$+"\\"$+ "\\..."$+...$+ "..." ? Is there somewhere wrong inside those lines above? Thank you very much for the help!

2 Answers


The string combination operator

The $+ operator combines strings. For example:

//Create 3 strings
a = "this";
b = "is a";
c = "string";

//Combine the strings
d = a $+ b $+ c;

//print the combined strings
print d;

should produce the following output:

thisis astring

In the code that you supplied, they are using the string combination operator along with the "\\" to create a path. For example:

root_dir = "C:\\Users\\MyUserName";

next_dir = "Mycode";

//Combine strings to create full path
full_path = root_dir $+ "\\" $+ next_dir;

print full_path;

should return:


Note that only one backslash is shown in the printed output (this is correct).

Why is the code failing?

Most likely the code is failing because not all of the paths exist. For example, if the code is trying to create a dataset in the directory C:\Users\Public\ProjectA\Data, but that directory does not exist, then the dataset creation will fail.

How can we fix it?

The simplest thing to do would be to check to see if the file handle is valid. The file handle must be a positive number, -1 means the file handle is bad. If the file handle is bad, we can print ourselves a message that tells us what file the program was trying to open.

//Create first part of path
opath = parapath $+ "\\mhrun" $+ mhrun;

//Add final part of path
opara = opath $+ "\\" $+ lmodel $+ lprior $+ "0" $+ subTstr $+ "pa0";

//Create empty dataset to write to later
create fhpara=^opara with PARAM, npara, 8;

//Check to see if 'create' was successful
if fhpara < 0;
   print "'create' failed trying to open the file:";
   print opara;
   print "stopping program....";

This message will tell you what file it was trying to open and you can check to see if the path exists. I would suspect that part of it does not. If you have problems with any of the other create or open calls in that code, you can do the same thing. Feel free to post any questions that come up.


Thank you so much! This is very helpful.

You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.

Try GAUSS for 14 days for FREE

See what GAUSS can do for your data

© Aptech Systems, Inc. All rights reserved.

Privacy Policy