#REDIRECT [[OPEN#File_Access_Modes]]

The [[OUTPUT]] file mode is used in an [[OPEN]] statement to send new data to files or ports.


{{PageSyntax}}
: [[OPEN]] {{Parameter|fileName$}} '''FOR''' [[OUTPUT]] '''AS''' #1


{{PageDescription}}
* OUTPUT mode erases all previous data in an existing file or clears a port receive buffer.
* Creates an empty file if the filename does not exist. Use [[APPEND]] if previous file data is to be preserved.
* Mode can use [[PRINT (file statement)|PRINT]], [[WRITE (file statement)|WRITE]] or [[PRINT USING (file statement)|PRINT USING]] to output file data.


{{PageExamples}}
''Example:'' Writes new data to a text file sequentially and reads it back to the program screen.
{{CodeStart}} '' ''
filename$ = "testfile.dat" 
x = 1: y = 2: z$ = "Three" 

{{Cl|OPEN}} filename$ {{Cl|FOR...NEXT|FOR}} {{Cl|OUTPUT}} {{Cl|AS}} #1 'opens and clears an existing file or creates new empty file 

{{Cl|WRITE (file statement)|WRITE}} #1, x, y, z$ 

{{Cl|CLOSE}} #1 

{{Cl|PRINT}} "File created with data. Press a key!" 

K$ = {{Cl|INPUT$}}(1) 'press a key 

{{Cl|OPEN}} filename$ {{Cl|FOR (file statement)|FOR}} {{Cl|INPUT (file mode)|INPUT}} {{Cl|AS}} #2 'opens a file to read it 

{{Cl|INPUT (file statement)|INPUT}} #2, a, b, c$ 

{{Cl|CLOSE}} #2 

{{Cl|PRINT}} a, b, c$ 

{{Cl|END}} '' ''
{{CodeEnd}}


{{PageSeeAlso}}
* [[APPEND]], [[RANDOM]]
* [[INPUT (file mode)]], [[BINARY]]
* [[WRITE]], [[INPUT (file statement)]]


{{PageNavigation}}
