WRITE (file statement): Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (Removed protection from "WRITE (file statement)")
No edit summary
Line 19: Line 19:


''Example:'' Writes new data to a text file sequentially and reads it back to the program screen.
''Example:'' Writes new data to a text file sequentially and reads it back to the program screen.
{{CodeStart}} '' ''
{{CodeStart}}
filename$ = "testfile.dat"  
filename$ = "testfile.dat"
x = 1: y = 2: z$ = "Three"  
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|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|WRITE (file statement)|WRITE}} #1, x, y, z$


{{Cl|CLOSE}} #1  
{{Cl|CLOSE}} #1


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


K$ = {{Cl|INPUT$}}(1) '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|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|INPUT (file statement)|INPUT}} #2, a, b, c$


{{Cl|CLOSE}} #2  
{{Cl|CLOSE}} #2


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


{{Cl|END}} '' ''
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}
: ''File content:'' [[WRITE]] string values will include quotation marks, but they are not required to read the file.
: ''File content:'' [[WRITE]] string values will include quotation marks, but they are not required to read the file.

Revision as of 03:00, 23 January 2023

The WRITE # file statement writes a list of comma separated variable values to a sequential file or port.


Syntax

Template:KWfilenumber&[, expressionList]


Description

  • filenumber& is the number of the file or device Template:KWed in the Template:KW or Template:KW modes. See: Template:KW.
  • expressionList is a comma-separated list of values to be written to the file or device.
  • WRITE can place any number and types of variable values needed in a file record separated by commas.
  • String values will have quotation marks although quotes are not required to read strings in CSV files with INPUT #.
  • Data files using WRITE normally will have the same number of values listed on each file line.
  • Data containing commas must be in quotation marks. Number commas are illegal!
  • WRITE created files are normally read with INPUT #.
  • CSV files created can be read by Excel using a .CSV file name extension. Strings may or may not include quotation marks.
  • Semicolons cannot be used in or following the WRITE statement!


Example: Writes new data to a text file sequentially and reads it back to the program screen.

filename$ = "testfile.dat"
x = 1: y = 2: z$ = "Three"

OPEN filename$ FOR OUTPUT AS #1 'opens and clears an existing file or creates new empty file

WRITE #1, x, y, z$

CLOSE #1

PRINT "File created with data. Press a key!"

K$ = INPUT$(1) 'press a key

OPEN filename$ FOR INPUT AS #2 'opens a file to read it

INPUT #2, a, b, c$

CLOSE #2

PRINT a, b, c$
WRITE a, b, c$

END
File content: WRITE string values will include quotation marks, but they are not required to read the file.
1,2,"Three"
Screen output: PRINT string values will not display enclosing quotes. WRITE screen displays will.
 1           2          Three
1,2,"Three"


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage