PRINT (file statement): Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (Removed protection from "PRINT (file statement)")
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 6: Line 6:




{{Parameters}}
{{PageParameters}}
* {{Parameter|fileNumber&}} is the file number of a file or device opened for writing. See {{KW|OPEN}}.
* {{Parameter|fileNumber&}} is the file number of a file or device opened for writing. See [[OPEN]].
* {{Parameter|expression}} is a numeric or string expression to be written to the file. Quotes will be removed from strings.
* {{Parameter|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.
* The print statement can be followed by a [[semicolon]] to stop the print cursor or a [[comma]] to tab the next print.
Line 27: Line 27:
** If a numeric literal is in scientific notation, the number is also written in scientific notation. [[PRINT USING (file statement)|PRINT #, USING]] can return actual rounded numerical values in string form.
** If a numeric literal is in scientific notation, the number is also written in scientific notation. [[PRINT USING (file statement)|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.
** The numerical value is always followed by a space character unless [[STR$]] is used to convert it to a string value.
* Whenever [[PRINT (file statement)|PRINT #]] moves the file cursor to a new file row, a carriage return character ({{KW|CHR$|CHR$(13)}}) followed by a line feed character ({{KW|CHR$|CHR$(10)}}) is written. The combination are referred to as the "CRLF" character.
* Whenever [[PRINT (file statement)|PRINT #]] moves the file cursor to a new file row, a carriage return character ([[CHR$|CHR$(13)]]) followed by a line feed character ([[CHR$|CHR$(10)]]) 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!'''
* '''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.
''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.

Latest revision as of 00:52, 25 January 2023

The PRINT # statement prints numeric or string expressions to a sequential file, IO port or device.


Syntax

PRINT #fileNumber&, [ [expression] [{;|,] ... ]


Parameters

  • fileNumber& is the file number of a file or device opened for writing. See OPEN.
  • 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 (CHR$(13)) followed by a line feed character (CHR$(10)) 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



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link