PRINT (file statement): Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
m (Removed protection from "PRINT (file statement)") |
No edit summary |
||
Line 32: | Line 32: | ||
''Example:'' Prints data to a text file sequentially and reads it back to the program screen as one line of text. | ''Example:'' Prints data to a text file sequentially and reads it back to the program screen as one line of text. | ||
{{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|PRINT (file statement)|PRINT}} #1, x, y, z$ | {{Cl|PRINT (file statement)|PRINT}} #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|LINE INPUT (file statement)|LINE INPUT}} #2, text$ | {{Cl|LINE INPUT (file statement)|LINE INPUT}} #2, text$ | ||
{{Cl|CLOSE}} #2 | {{Cl|CLOSE}} #2 | ||
{{Cl|PRINT}} text$ | {{Cl|PRINT}} text$ | ||
{{Cl|WRITE}} text$ | {{Cl|WRITE}} text$ | ||
{{Cl|END}} | {{Cl|END}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
: ''File content:'' [[PRINT (file statement)|PRINT]] string file values will not include the enclosing quotation marks but can be read by [[LINE INPUT (file statement)|LINE INPUT]] as text. | : ''File content:'' [[PRINT (file statement)|PRINT]] string file values will not include the enclosing quotation marks but can be read by [[LINE INPUT (file statement)|LINE INPUT]] as text. | ||
{{TextStart}} 1 2 Three {{TextEnd}} | {{TextStart}} 1 2 Three {{TextEnd}} | ||
: ''Screen output:'' [[PRINT]] string values will not display enclosing quotation marks. [[WRITE]] screen displays will. | : ''Screen output:'' [[PRINT]] string values will not display enclosing quotation marks. [[WRITE]] screen displays will. |
Revision as of 02:23, 23 January 2023
The PRINT # statement prints numeric or string expressions to a sequential file, IO port or device.
Syntax
- PRINT #fileNumber&, [ [expression] [{;|,] ... ]
- fileNumber& is the file number of a file or device opened for writing. See Template:KW.
- expression is a numeric or string expression to be written to the file. Quotes will be removed from strings.
- The print statement can be followed by a semicolon to stop the print cursor or a comma to tab the next print.
Usage:
- STRING values will be stripped of leading and trailing quotation marks when printed to the file. Use CHR$(34) to add quotes to a file.
- separator is used to separate multiple expressions and specifies how the file cursor is to be moved before writing the next expression. It can be one of the following:
- semi-colon (;) - specifies that the print cursor stop immediately after the print. A subsequent print will start there.
- comma (,) - specifies that the file cursor is to move to the next 14-column tab-stop. If the file cursor is at column 56 or greater, it is moved to the next file row at column 1.
- If separator is not used at the end of the expression list, the file cursor moves to the next file row at column 1.
- PRINT # can use the + concatenation operator or semicolons to combine strings.
- SPC(n%) - specifies that n% space characters will be written in a print.
- TAB(column%) - specifies that the file cursor is to move to a column number column%. If the file cursor is beyond that column, it is moved to that column on the next file row.
- When printing literal or variable numerical values the following rules apply:
- If the value is positive, the number is prefixed with a space character, otherwise, the number is prefixed with a negative sign (-).
- If the value is an INTEGER (whole number), no decimal point or fractional part will be written.
- If the value is not an integer (whole number) and has zero for a coefficient, no leading zero is written. For example, -0.123 is written as "-.123 "
- If a numeric literal is in scientific notation, the number is also written in scientific notation. PRINT #, USING can return actual rounded numerical values in string form.
- The numerical value is always followed by a space character unless STR$ is used to convert it to a string value.
- Whenever PRINT # moves the file cursor to a new file row, a carriage return character (Template:KW) followed by a line feed character (Template:KW) is written. The combination are referred to as the "CRLF" character.
- Note: RANDOM and BINARY files are not affected by PRINT # statements to them and will create a syntax error in QB64!
Example: Prints data to a text file sequentially and reads it back to the program screen as one line of text.
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 PRINT #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 LINE INPUT #2, text$ CLOSE #2 PRINT text$ WRITE text$ END |
- File content: PRINT string file values will not include the enclosing quotation marks but can be read by LINE INPUT as text.
1 2 Three |
- Screen output: PRINT string values will not display enclosing quotation marks. WRITE screen displays will.
1 2 Three " 1 2 Three" |
See also