# Basic Input and Output

> Source: https://manual.pvxplus.com/pxplus-user-guide/programming-constructs/basic-input-and-output/overview

The **[INPUT](https://manual.pvxplus.com/directives/input)** directive is used to enter data interactively while the program is running. It issues prompts to the user and processes the responses. **PRINT** statements may be used to format and output printable data to a monitor or printer, as well as into a file. PxPlus I/O statements need to reference a ***channel number*** in order to access a device or file. The relationship between a channel and the physical file or device must first be established via the **[OPEN](https://manual.pvxplus.com/directives/open)** directive.

Other I/O directives (**READ**, **WRITE**, etc.) are used specifically for transferring data in and out of files. See **[File Processing Directives](https://manual.pvxplus.com/pxplus-user-guide/file-handling/processing-data-files/file-processing-directives)**.

For information on I/O processing in a graphical user interface environment, see **[Graphical User Interfaces](https://manual.pvxplus.com/pxplus-user-guide/graphical-user-interfaces/introduction)**.

## Opening/Closing Devices and Files

Most input/output operations in PxPlus require a channel number (*chan*) to identify the connection to a specific device, interface or file. These are established using the **[OPEN](https://manual.pvxplus.com/directives/open)** directive, which associates a *logical* number (normally between 0 and 127) with a *physical* file or device. Once the *chan* is defined, the program will be able to access the input or generate output simply by referencing that number.

In theory, all I/O statements use channel numbers; however, the *console* (keyboard and display screen) is defined as channel 0 by default and may be omitted from **INPUT** or **PRINT** statements intended for immediate display.

For files, the **OPEN** process also sets a pointer to the beginning of the opened file and allocates system resources. The actual number of files that can be opened at any one time depends on the operating system. See **[File Handling](https://manual.pvxplus.com/pxplus-user-guide/file-handling/introduction)** for information on using **OPEN** for file I/O operations.

The basic syntax for an

**OPEN** statement is:

```
OPEN (chan[ ,fileopt ]) string$
```

***Where:***

|  |  |
| --- | --- |
| *chan* | Logical channel number to be assigned. |
| *fileopt* | Various options used for controlling the contents and characteristics of a data file. See **[Processing Data Files](https://manual.pvxplus.com/pxplus-user-guide/file-handling/processing-data-files/overview)**. |
| *string$* | Name of the file or device to open. The string expression can include a specialty file name or file tag (e.g. **\*MEMORY\***, **\[RPC\]**, etc.). |

Several **OPEN** keywords are also available for specific types of file access operations, including **INPUT**, **LOCK**, **PURGE** and **LOAD**.

Most I/O operations begin with an **[OPEN](https://manual.pvxplus.com/directives/open)** directive to establish the relationship with the target device or file.

***Example:***

```
PRINT "Today's date is ",DAY

OPEN (1)"*WINPRT*;HP Laser Jet;Orientation=Landscape"

PRINT (1)@(0),"Customer",@(45)," Balance"
```

In this example, the first statement simply outputs text to the screen, which is the default syntax for **PRINT**. The second **PRINT** statement outputs text to ***channel*** (1), which is the identity of a printer made available for use via the preceding **[OPEN](https://manual.pvxplus.com/directives/open)** directive.

The **[CLOSE](https://manual.pvxplus.com/directives/close)** directive closes the connection to one or more files/devices and allows their channel number(s) to be reused. The basic syntax is:

```
CLOSE {(*) | (chan) [, (chan) ...]}
```

***Where:***

|  |  |
| --- | --- |
| *\** | An *asterisk* denotes "all **OPEN** local channels" *except* channel 0. |
| *chan* | Channel number. |

Note:  
Local files are closed automatically on a BEGIN or END statement. All files are closed at the end of a user session or whenever a [START](https://manual.pvxplus.com/directives/start) directive is issued.

## See Also

**[Input Statements](https://manual.pvxplus.com/pxplus-user-guide/programming-constructs/basic-input-and-output/input-statements)  
[Output Statements](https://manual.pvxplus.com/pxplus-user-guide/programming-constructs/basic-input-and-output/output-statements)**
