# uopen command

The uopen command opens a given file name to a file
 variable.

## Format

```
UOPEN <path> [FOR {READWRITE|READ|WRITE}] TO FileHandle THEN statement(s)
ELSE statement(s)
```

## Parameters

| path | File expressed by the valid path name, within quotes. |
| --- | --- |
| FileHandle | Must be used for all future references to the file. |

## Description

The file pointer is positioned at the beginning of the file. The path points to a path naming
 a file. If an error occurs or the file does not exist, the ELSE clause executes.

 One reason that the uopen command may fail is that the user attempting to
 open the file does not have the necessary permissions. For example, if the file contains
 confidential information that the user is not authorized to read, the uopen
 command does not open the file. The ELSE clause executes instead.

 FileHandle is a variable that is returned from the uopen command and must
 be used for subsequent accesses to this file. The *FileHandle* variable is the
 file descriptor of the file on the host system. If the file cannot be opened, the ELSE clause
 executes.

## Example(s)

FILEDES1 contains the file descriptor that allows the user to access the
 \books\chap4.txt file. In the following statement, the file is opened for
 READ only. Any attempt to write to the file using this file descriptor results in an error.

```
UOPEN “c:\books\chap4.txt” FOR READ TO FILEDES1 ELSE STOP
```

 The following statement opens the chap4.txt file located in the current
 directory. FILEDES2 contains the file descriptor necessary to open the
 chap4.txt file. The file is opened for WRITE only. If the UREAD command
 is used, an error results. If the file cannot be opened for any reason, the message in the ELSE
 clause displays.

```
UOPEN “chap4.txt” FOR WRITE TO FILEDES2 ELSE PRINT “CAN NOT OPEN FILE!”
```

 The next statement opens the chap4.txt file located in the parent
 directory of the current directory. If the file can be opened for reading and writing, FILEDES3
 contains the file descriptor allowing access to the file. If an error occurs, the ELSE clause
 executes.

```
UOPEN “..\chap4.txt” FOR READWRITE TO FILEDES3 ELSE STOP
```

 The last statement opens the chap4.txt file for reading and writing.
 After the file is opened, FILEDES4 contains the file descriptor that allows access to the
 chap4.txt file. If an error occurs, the ELSE clause executes.

```
UOPEN “chap4.txt” TO FILEDES4 ELSE STOP
```

## See also

- [O/S interoperability commands and functions](https://d3codex.com/pickbasic-flashbasic/os-interoperability-commands/)
- [uclose command](https://d3codex.com/pickbasic-flashbasic/uclose-command/)
- [ucreate command](https://d3codex.com/pickbasic-flashbasic/ucreate-command/)
- [udelete command](https://d3codex.com/pickbasic-flashbasic/udelete-command/)
- [uerror() function](https://d3codex.com/pickbasic-flashbasic/uerror-function/)
- [uexecute command](https://d3codex.com/pickbasic-flashbasic/uexecute-command/)
- [ulock command](https://d3codex.com/pickbasic-flashbasic/ulock-command/)
- [ulseek() function](https://d3codex.com/pickbasic-flashbasic/ulseek-function/)
- [umessage command](https://d3codex.com/pickbasic-flashbasic/umessage-command/)
- [uread command](https://d3codex.com/pickbasic-flashbasic/uread-command/)
- [ureadline() function](https://d3codex.com/pickbasic-flashbasic/ureadline-function/)
- [usystem() function](https://d3codex.com/pickbasic-flashbasic/usystem-function/)
- [uwaitfor command](https://d3codex.com/pickbasic-flashbasic/uwaitfor-command/)
- [uwrite command](https://d3codex.com/pickbasic-flashbasic/uwrite-command/)

---
Source: https://d3codex.com/pickbasic-flashbasic/uopen-command/ - part of the D3Codex reference.
