Additional Topics > External Development Guide > IDLInternals - Files and Input Output > Allocating and Freeing File Units

# Allocating and Freeing File Units

System routines must allocate and deallocate file units in order to avoid conflicts. When writing IDL procedures, the GET_LUN and FREE_LUN procedures are used. When writing system-level routines, you can access the same routines by calling IDL_FileGetUnit() and IDL_FileFreeUnit().

## IDL_FileGetUnit()

Use IDL_FileGetUnit() to allocate file units.

void IDL_FileGetUnit(int argc, IDL_VPTR *argv)

### argc

argc should always be 1.

### argv

argv[0] contains an IDL_VPTR to the IDL_VARIABLE that will be filled in with the resulting unit number.

## IDL_FileFreeUnit()

Use IDL_FileFreeUnit() to free file units.

void IDL_FileFreeUnit(int argc, IDL_VPTR *argv)

### argc

argc gives the number of elements in argv.

### argv

argv should contain scalar integer values giving the Logical Unit Numbers of the file units to be returned.

## Coding Patterns

The following code pattern demonstrates how these functions might be used to open and close a file named `junk.dat`:

IDL_VPTR argv[2];

IDL_VARIABLE unit;

IDL_VARIABLE name;

.

.

.

/* Allocate a file unit. */

argv[0] = &unit;

unit.type = IDL_TYP_LONG;

unit.flags = 0;

IDL_FileGetUnit(1, argv);

/* Set up the file name */

name.type = IDL_TYP_STRING;

name.flags = IDL_V_CONST;

name.value.str.s = "junk.dat";

name.value.str.slen = sizeof("junk.dat") - 1;

name.value.str.stype = 0;

argv[1] = &name;

.

.

.

IDL_FileOpen(2, argv, (char *) 0, IDL_OPEN_R, 0, 1, 0);

/* Perform any required actions. */

.

.

.

/* Free the file unit and close the file. */

IDL_FileFreeUnit(1, argv);

## Additional Information

See GET_LUN and FREE_LUN in the IDL online help for additional details about these functions.