FILEATTR
- 来源: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgFileattr
- 最后更新: 2016-03-13
返回已打开文件号的相关信息
语法
declare function FileAttr ( byval filenum as long, byval returntype as long = 1 ) as integer用法
#include "file.bi"
result = FileAttr( filenum, [ returntype ] )或
#include "vbcompat.bi"
result = FileAttr( filenum, [ returntype ] )参数
filenum
通过 Open 打开的文件或设备的文件号。
returntype
指定要返回的信息类型的整数值。
返回值
返回与返回类型相关的值;出错时返回 0。
说明
根据提供的 returntype 返回文件号的相关信息:
| 值 | 说明 | 常量 |
|---|---|---|
| 1 | 文件模式 | fbFileAttrMode |
| 2 | 文件句柄 | fbFileAttrHandle |
| 3 | 编码 | fbFileAttrEncoding |
当 returntype = 1(fbFileAttrMode)时,返回值为以下一个或多个值之和:
| 值 | 文件模式 | 常量 |
|---|---|---|
| 1 | Input | fbFileModeInput |
| 2 | Output | fbFileModeOutput |
| 4 | Random | fbFileModeRandom |
| 8 | Append | fbFileModeAppend |
| 32 | Binary | fbFileModeBinary |
当 returntype = 2(fbFileAttrHandle)时,返回值为 C 运行时为文件类型设备提供的文件句柄。
仅 Windows:当 returntype = 2(fbFileAttrHandle)时,COM 设备返回的值为首次打开设备时 CreateFile() 返回的句柄;LPT 设备返回的值为首次打开设备时 OpenPrinter() 返回的句柄。该句柄值可传递给其他 Windows API 函数。
仅 Linux:当 returntype = 2(fbFileAttrHandle)时,COM 设备返回的值为首次打开设备时 open() 返回的文件描述符。
当 returntype = 3(fbFileAttrEncoding)时,返回值为以下之一:
| 值 | 编码 | 常量 |
|---|---|---|
| 0 | Ascii | fbFileEncodASCII |
| 1 | UTF-8 | fbFileEncodUTF8 |
| 2 | UTF-16 | fbFileEncodUTF16 |
| 3 | UTF-32 | fbFileEncodUTF32 |
示例
vb
#include "vbcompat.bi"
#include "crt.bi"
Dim f As FILE Ptr, i As Integer
'' Open a file and write some text to it
Open "test.txt" For Output As #1
f = Cast( FILE Ptr, FileAttr( 1, fbFileAttrHandle ))
For i = 1 To 10
fprintf( f, !"Line %i\n", i )
Next i
Close #1
'' re-open the file and read the text back
Open "test.txt" For Input As #1
f = Cast( FILE Ptr, FileAttr( 1, fbFileAttrHandle ))
While feof(f) = 0
i = fgetc(f)
Print Chr(i);
Wend
Close #1与 QB 的差异
returntype= 1 时无差异- QBasic 和 16 位 Visual Basic 在
returntype= 2 时返回 DOS 文件句柄 returntype= 3 是 FreeBASIC 新增的
另请参阅
Open
返回 目录