Print # 语句
将显示格式的数据写入顺序文件。
INFO
本页面记录的是Print #语句(文件I/O)。不相关的Debug.Print语句在调试时写入立即窗口。
语法:
Print # filenumber , [ outputlist ]
- filenumber
任何有效的文件号。
- outputlist
可选 要打印的表达式或表达式列表。outputlist的设置为:
[ { Spc(n) | Tab [ (n) ] } ] [ expression ] [ charpos ]
- Spc(n)
- 用于在输出中插入空格字符,n为要插入的空格字符数。
- Tab(n)
- 用于将插入点定位到绝对列号,n为列号。使用不带参数的Tab将插入点定位到下一个打印区的开头。
- expression
- 要打印的数值表达式或字符串表达式。
- charpos
- 指定下一个字符的插入点。使用分号将插入点定位到最后显示字符之后。使用Tab(n)将插入点定位到绝对列号。使用不带参数的Tab将插入点定位到下一个打印区的开头。如果省略charpos,下一个字符打印在下一行。
使用**Print #**写入的数据通常用Line Input #或Input #从文件中读取。
当省略outputlist且文件号后仅跟列表分隔符时,向文件打印一个空行。
多个表达式可以用空格或分号分隔。空格与分号效果相同。
对于Boolean数据,打印True或False。True和False关键字不会根据区域设置进行翻译。
Date数据使用系统识别的标准短日期格式写入文件。当日期或时间部分缺失或为零时,仅写入提供的部分。
如果outputlist数据为Empty,则不向文件写入任何内容。但如果outputlist数据为Null,则向文件写入Null。
对于Error数据,输出显示为Error errorcode。Error关键字不会根据区域设置进行翻译。
使用**Print #**写入文件的所有数据都具有国际化感知能力;即数据使用适当的小数分隔符正确格式化。
因为Print #将数据的映像写入文件,所以数据必须正确分隔才能正确打印。当使用不带参数的Tab将打印位置移动到下一个打印区时,**Print #**也会将打印字段之间的空格写入文件。
INFO
当稍后使用**Input #语句从文件读取数据时,请使用Write #语句代替Print #语句将数据写入文件。使用Write #可通过正确分隔来确保每个独立数据字段的完整性,以便使用Input #读回。使用Write #**还能确保在任何区域设置中都能正确读取。
示例
本示例使用**Print #**语句将数据写入文件。
Open "TESTFILE" For Output As #1 ' Open file for output.
Print #1, "This is a test" ' Print text to file.
Print #1, ' Print blank line to file.
Print #1, "Zone 1"; Tab; "Zone 2" ' Print in two print zones.
Print #1, "Hello"; " "; "World" ' Separate strings with space.
Print #1, Spc(5); "5 leading spaces " ' Print five leading spaces.
Print #1, Tab(10); "Hello" ' Print word at column 10.
' Assign Boolean, Date, Null and Error values.
Dim MyBool, MyDate, MyNull, MyError
MyBool = False : MyDate = #February 12, 1969# : MyNull = Null
MyError = CVErr(32767)
' True, False, Null, and Error are translated using locale settings of
' your system. Date literals are written using standard short date
' format.
Print #1, MyBool; " is a Boolean value"
Print #1, MyDate; " is a date"
Print #1, MyNull; " is a null value"
Print #1, MyError; " is an error value"
Close #1 ' Close file.