error G0122:Bad file handle


When I run the var_vardraw.g written by Frank Schorfheide in his "BAYESIAN ANALYSIS OF DSGE MODELS (2007)", I got this problem:

C:\Users\n\Desktop\Frank Shorfheide\BAYESIAN ANALYSIS OF DSGE MODELS\20050812 ds(34):error G0122:Bad file handle

Can you tell me why?


Zixiang Zhu

5 Answers


The error G0122: Bad file handle indicates that a function is trying to access a file using a file handle that was created with either the GAUSS command open or fopen. However, the open or fopen failed to get a file handle and returned a -1.

The most common reason for open or fopen to fail to get a proper file handle is that the file does not exist in the location in which the function is looking for the file.

I would recommend putting a breakpoint on the line of the error and using the debugger to trace the location of the failed open or fopen.



I follow your way and use the debugger to trace the line 34

"drawrow = readr( fhdraw, nburn);"

Does it mean the file fhdraw not exist?

Zixiang Zhu


In the line of code:

drawrow = readr( fhdraw, nburn);

The variable fhdraw should be a file handle to a GAUSS dataset. A file handle is just a scalar value that tells GAUSS where to find the underlying file descriptor. If the value of a file handle is -1, then GAUSS could not get a proper file handle for the file. Generally this means that either: 1) The file does not exist 2) It is in a different directory or 3) That you do not have permissions to read the file (less likely).

Look in the lines above the code snippet for a line that looks something like this:

open fhdraw = ^fname for read;

Then look in your GAUSS working directory to see if this file exists or not. You can use the GAUSS function filesa to list files that match a string. For example, you can tell GAUSS to print the names of all files in your current directory that end with .dat (i.e. GAUSS datasets) with this command:


If GAUSS does not print out the name of the dataset you are trying to open, then it is not present.


Thanks very much!

But I'm sorry that I still don't know why the file doesn't exist. I followed the readme instruction, attach file library files contained in \usersrc\ to my user library and install procedures to solve linear rational expectations models.

All the main proram ran well except var_parapmom.g:

/* filename: bparapmom.g

** description: posterior mode batch
** created: 11/05/01


mhrun = "1";

nburn = 101;
/* nburn = 1; */
/* Number of initial draws to be discarded
** use nburn = 1 for draws from prior
** use the equivalent of 20 percent for draws from posterior */

hpdprob = 0.90;
block_size = 10000;

/** only the following 2 lines are model dependent **/
#include nkmp_mod.src;
#include nkmp_para.src;
#include nkmp_spec.g;
#include var_spec.g;

#include pathspec.g;

#include gensys.src;
#include dsgesel.src;
#include matop.src;

para = pmsv(msel);
npara = rows(para);
{para,para_names} = psel(para,msel);

** Iterate over lambda

Tind = 1;
do until Tind > 1;

* Import data series

lamind = 1;
do until lamind > rows(lamgrid);

lambda = lamgrid[lamind];
gosub runproc;

lamind = lamind+1;


Tind = Tind+1;



"Index L,T" lamind Tind;
"LAMBDA = " lambda;

#include p_parapmom.g



And the problem result from p_parapmom.g:


/* filename: parapmom.g
** description:
** created: 11/05/01

** Define lambda indices

if lamind < 10;
llam = "0" $+ ftos(lamind,"%*.*lf",1,0);
llam = ftos(lamind,"%*.*lf",2,0);

if Tind < 10;
lsamp = "0" $+ ftos(Tind,"%*.*lf",1,0);
lsamp = ftos(Tind,"%*.*lf",2,0);

** Load Parameter Draws from MH Output

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

ldraw = lpath $+ "\\" $+ llam $+ lsamp $+ "pa" ;
odraws = opath $+ "\\" $+ llam $+ lsamp $+ "pas" ;

open fhdraw = ^ldraw for read;

drawrow = readr( fhdraw, nburn); 
drawdim = cols(drawrow);
drawrow = seekr( fhdraw, nburn);



I think this is most likely happening, because of something going wrong in the file 'pathspec.g'. Place a break point at the top of that file and see where it is trying to create directories. If the directory creation fails, then nothing will work. Looking in 'pathspec.g' will also show you where the code is saving files.

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