Skip to content

FILEATTR


返回已打开文件号的相关信息

语法

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)时,返回值为以下一个或多个值之和:

文件模式常量
1InputfbFileModeInput
2OutputfbFileModeOutput
4RandomfbFileModeRandom
8AppendfbFileModeAppend
32BinaryfbFileModeBinary

returntype = 2(fbFileAttrHandle)时,返回值为 C 运行时为文件类型设备提供的文件句柄。

仅 Windows:当 returntype = 2(fbFileAttrHandle)时,COM 设备返回的值为首次打开设备时 CreateFile() 返回的句柄;LPT 设备返回的值为首次打开设备时 OpenPrinter() 返回的句柄。该句柄值可传递给其他 Windows API 函数。

仅 Linux:当 returntype = 2(fbFileAttrHandle)时,COM 设备返回的值为首次打开设备时 open() 返回的文件描述符。

returntype = 3(fbFileAttrEncoding)时,返回值为以下之一:

编码常量
0AsciifbFileEncodASCII
1UTF-8fbFileEncodUTF8
2UTF-16fbFileEncodUTF16
3UTF-32fbFileEncodUTF32

示例

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

返回 目录

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系