FileAttr
 
Returns information about an open file number

Syntax

Declare Function FileAttr ( ByVal filenum As Long, ByVal returntype As Long = 1 ) As Long

Usage

#include "file.bi"
result = FileAttr( filenum, [ returntype ] )

or

#include "vbcompat.bi"
result = FileAttr( filenum, [ returntype ] )

Parameters

filenum
The file number of a file or device opened with Open
returntype
An integer value indicating the type of information to return.

Return Value

A value associated with the return type, otherwise 0 on error.

Description

Information about the file number is returned based on the supplied returntype
ValueDescriptionconstant
1File ModefbFileAttrMode
2File HandlefbFileAttrHandle
3EncodingfbFileAttrEncoding


For File Mode, returntype = 1 (fbFileAttrMode) the return value is the sum of one or more of the following values:
ValueFile ModeConstant
1InputfbFileModeInput
2OutputfbFileModeOutput
4RandomfbFileModeRandom
8AppendfbFileModeAppend
32BinaryfbFileModeBinary


For File Handle, returntype = 2 (fbFileAttrHandle), the return value is the file handle as supplied by the C Runtime for file-type devices.

On Windows only: For File Handle, returntype = 2 (fbFileAttrHandle), the value returned for COM devices is the handle returned by CreateFile() when the device was first opened. The value returned for LPT devices is the handle returned by OpenPrinter() when the device was first opened. This handle value can be passed to other Windows API functions.

On Linux only: For File Handle, returntype = 2 (fbFileAttrHandle), the value returned for COM devices is the file descriptor returned by open() when the device was first opened.

For Encoding, returntype = 3 (fbFileAttrEncoding), the return value is one of the following values:
ValueEncodingConstant
0AsciifbFileEncodASCII
1UTF-8fbFileEncodUTF8
2UTF-16fbFileEncodUTF16
3UTF-32fbFileEncodUTF32


Example

#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


Differences from QB

  • None for returntype = 1
  • QBasic and 16-bit Visual Basic returned DOS file handle for returntype = 2
  • returntype = 3 is new to FreeBASIC

See also