# %fopen() function

The %fopen() function opens the host
OS file designated by *str* and associates a stream
with it.

## Syntax

```
stream = (char*)%fopen(str, type)
```

## Parameter(s)

| str | Specifies the file to open. |
| --- | --- |
| type | Character string having one of these values: |
| r | Opens for reading only. |
| r+ | Opens for update. |
| w | Truncates or creates for writing only. |
| w+ | Truncates or creates for update. |
| a | Append. Opens for writing at the end of file or creates for writing. |
| a+ | Append. Opens for writing at the end of file or creates for update. |

## Description

The stream is returned as a
pointer to a character or 0 if an error occurred. system(0) contains the error number.

Note: In D3, files are closed automatically
when the FlashBASIC programs terminate. However, it is strongly recommended
that you close all descriptors explicitly.

## Example(s)

```
stream=(char*)%fopen(’fname’,"w+")
if stream=0 then
 crt ’Cannot open stream. errno=’:system(0)
end
```

## See also

- [%fclose() function](https://d3codex.com/pickbasic-flashbasic/percent-fclose-function/)
- [%fdopen() function](https://d3codex.com/pickbasic-flashbasic/percent-fdopen-function/)
- [%fgetc() function](https://d3codex.com/pickbasic-flashbasic/percent-fgetc-function/)
- [%fgets() function](https://d3codex.com/pickbasic-flashbasic/percent-fgets-function/)
- [%fprintf() function](https://d3codex.com/pickbasic-flashbasic/percent-fprintf-function/)
- [%fputc() function](https://d3codex.com/pickbasic-flashbasic/percent-fputc-function/)
- [%freopen() function](https://d3codex.com/pickbasic-flashbasic/percent-freopen-function/)
- [FlashBASIC C functions overview](https://d3codex.com/pickbasic-flashbasic/flashbasic-c-functions-overview/)
- [cfunction statement](https://d3codex.com/pickbasic-flashbasic/cfunction-statement/)

---
Source: https://d3codex.com/pickbasic-flashbasic/percent-fopen-function/ - part of the D3Codex reference.
