Low-Level File Access

Low-Level File Access Functions

FClose Closes a file opened by low-level access
FCreate Creates a file for low-level access
FErase Erase any file from disk by low-level access
FError Reports the error status of low-level file functions
FOpen Open a file for low-level access
FRead Reads bytes from a file opened with low-level access
FReadStr Reads a string from a file opened with low-level access
FRename Renames a file by low-level access
FSeek Positions the file pointer in a file opened with low-level access
FWrite Writes characters to a file opened with low-level access

HB_FTempCreateEx

HB_FTempCreateEx

Creates and opens a temporary file.

Syntax

HB_FTempCreateEx( @<cFileName,;
                    <cTempDir>,;
                    <cPrefix>,;
                    <cFileExtension> --> <nFileHandle>

Arguments

 @<cFileName> :must be passed by reference. It receives the file name of the created temporary  file as a character string.

<cTempDir> : A character string specifying the directory where to create the temporary file. It defaults to the operating       system variable SET TEMP or SET TMP. Operating system variables can be queried with function HB_GetEnv().

<cPrefix> : A character string of three characters specifying the prefix to use for the name of the temporary file. It defaults  to the string “hb”.

<nFileExtension> : A character value specifying extension of the temporary file.

Returns

<nFileHandle> : a numeric value > 0 when the temporary file is successfully created. This is the file handle of the created temporary file.

Description

Function HB_FTempCreateEx() creates a temporary file and opens it. The return value is the file handle of the temporary file. It can be used with low level file functions, such as FWrite() to store data in the file. Temporary files are often required in an application.

HB_FTempCreateEx() guarantees that the file name of the newly created file is unique, so that no existing file will be overwritten. To obtain the file name of the temporary file, parameter <cFileName> must be passed by reference. It can then later be passed to function FErase() in order to remove the temporary file from disk.

HB_FTempCreateEx() is similar to HB_FTempCreate(), but allows to control the extension. Note, that the ordering of parameters had slightly changed compared to hb_fsCreateTemp().

Example

      PROC TestHBFTempEx()

         LOCAL nFileHandle
         LOCAL cFileName

         nFileHandle := HB_FTempCreateEx( @cFileName,;   // File Name
                                          ,;             // <cTempDir>
                                          ,;             // <cPrefix>
                                          '.tmp' )       // file extension

         IF nFileHandle > 0
            ? 'Temp file builded with name :', cFileName
            WAIT
            FClose( nFileHandle )
            FErase( cFileName )
         ELSE
            ALERT( "Couldn't build temp file"  )
         ENDI

      RETU // TestHBFTempEx()

Seealso

TempFile(), HB_FTempCreate(), FCreate(), FErase(), FWrite(), HB_GetEnv()

HB_FTempCreate

HB_FTempCreate

Creates and opens a temporary file.

Syntax

      HB_FTempCreate( [<cTempDir>] , ;
                         [<cPrefix>]  , ;
                         [<nFileAttr>], ;
                         [@<cFileName>]  ) --> <nFileHandle>

Arguments

<cTempDir> : An optional character string specifying the directory where to create the temporary file. It defaults to the operating system variable SET TEMP or SET TMP. Operating system variables can be queried with function HB_GetEnv().

<cPrefix> : An optional character string of three characters specifying the prefix to use for the name of the temporary file. It defaults to the string “hb”.

<nFileAttr> : An optional numeric value defining how to create and open the temporary file. See function FCreate() for a description of <nFileAttr>.

@<cFileName> : If specified, <cFileName> must be passed by reference. It receives the file name of the created temporary file as a character string.

Returns

<nFileHandle> : a numeric value > 0 when the temporary file is successfully created. This is the file handle of the created temporary file.

Description

Function HB_FTempCreate() creates a temporary file and opens it. The return value is the file handle of the temporary file. It can be used with low level file functions, such as FWrite() to store data in the file.

Temporary files are often required in an application. HB_FTempCreate() guarantees that the file name of the newly created file is unique, so that no existing file will be overwritten. To obtain the file name of the temporary file, parameter <cFileName> must be passed by reference. It can then later be passed to function FErase() in order to remove the temporary file from disk.

Example

      PROC TestHBFTemp()

         LOCAL nFileHandle
         LOCAL cFileName

         nFileHandle := HB_FTempCreate( ,;   // <cTempDir>
                                        ,;   // <cPrefix>
                                        ,;   // <nFileAttr>
                                        , @cFileName )

         IF nFileHandle > 0
            ? 'Temp file builded with name :', cFileName
            WAIT
            FClose( nFileHandle )
            FErase( cFileName )
         ELSE
            ALERT( "Couldn't build temp file"  )
         ENDI

      RETU // TestHBFTemp()

Seealso

TempFile(), HB_FTempCreateEx(), FCreate(), FErase(), FWrite(), HB_GetEnv()

TempFile

TempFile

Creates a file for temporary use

Syntax

      TempFile([<cDirectory>], [<cExtension>], [<nFileAttr>])
             --> <cFileName>

Arguments

<cDirectory> : The drive and directory where the temporary file is to be created. The default is the system/user temp directory.

<cExtension> : The file extension for the file. The default is no extension.

<nFileAttr> : A file attribute for the file.

Returns

<cFileName> : The name of the temporary file. If no file is created, the function returns a null string.

Description

If you only need a file for a short time and want to avoid conflicts with existing files, use TempFile() to create a file with a unique name. Names created by TempFile() use the system date and system time. You can hide the file, if you specify an attribute. Attributes are coded as follows:

         File Attribute Coding
         ------------------------------------------------------------------------
         Value   Symb. constants     Definition
         ------------------------------------------------------------------------
         0       FA_NORMAL
         1       FA_READONLY         READ ONLY
         2       FA_HIDDEN           HIDDEN (Hidden files)
         4       FA_SYSTEM           SYSTEM (System files)
         32      FA_ARCHIVE          ARCHIVE (Changed since last backup)
         ------------------------------------------------------------------------

Notes

. The temporary file name is always 8 characters long and consists exclusively of numbers with a period, if no extension has been specified. The file is created with a length of 0 bytes.

Examples

      .  Create a temporary file in the system/user temp  directory:

         cTempFile := TempFile()

      .  Create a temporary file in the root directory of the E: drive:

         cTempFile := TempFile("E:\")

      .  Create a temporary file with a .TMP extension and a HIDDEN
         attribute in the root directory of the C: drive:

         cTempFile := TempFile("C:\", "TMP", 2)

Seealso

HB_FTempCreate(), HB_FTempCreateEx(), FCreate(), FErase(), FWrite(), HB_GetEnv()

Harbour All Functions – F

Fact

Fahrenheit

FClose()
FCount()
FCreate()
FErase()
FError()
FieldBlock()

FieldDeci()

FieldGet()

FieldName()
FieldPos()
FieldPut()

FieldSize()
FieldType()

FieldWBlock()

File()
FLock()

Floor

FOpen()

Found()

FRead()
FReadStr()
FRename()
FSeek()

FToC

FV

FWrite()

RENAME

RENAME

Changes the name of a specified file

Syntax

      RENAME <cOldFile> TO <cNewFile>

Arguments

<cOldFile> Old filename

<cNewFile> New Filename

Description

This command changes the name of <cOldFile> to <cNewFile>. Both <cOldFile> and <cNewFile> must include a file extension. This command if not affected by the SET PATH TO or SET DEFAULT TO commands; drive and directory designates must be specified if either file is in a directory other then the default drive and directory.

If <cNewFile> id currently open or if it previously exists, this command will not perform the desired operation.

Examples

      RENAME hello.txt TO hello.old

Compliance

Clipper

Files

Library is rtl

Seealso

CURDIR(), ERASE, FILE(), FERASE(), FRENAME()

FRename()

FRENAME()

Renames a file by low-level access

Syntax

      FRENAME( <cOldFile>, <cNewFile> ) --> nSuccess

Arguments

<cOldFile> Old filename to be changed

<cNewFile> New filename

Returns

<nSuccess> If successful, a 0 will be returned otherwise, a -1 will be returned.

Description

This function renames the specified file <cOldFile> to <cNewFile>. A filename and/or directory name may be specified for either parameter. However, if a path is supplied as part of <cNewFile> and this path is different from either the path specified in <cOldFile> or (if none is used) the current drive and directory, the function will not execute successfully.

Neither parameter is subject to the control of the SET PATH TO or SET DEFAULT TO commands. In attempting to locate the file to be renamed, this function will search the default drive and directory or the drive and path specified in <cOldFile>. It will not search directories named by the SET PATH TO and SET DEFAULT TO commands or by the DOS PATH statement.

If the file specified in <cNewFile> exists or the file is open, the function will be unable to rename the file. If the function is unable to complete its operation, it will return a value of -1. If it is able to rename the file, the return value for the function will be 0. A call to FERROR() function will give additional infor- mation about any error found.

Examples

      #include "fileio.ch"
      nResult := FRename( "x.txt", "x1.txt" )
      IF nResult == F_ERROR
         ? "File could not be renamed."
      ENDIF

Compliance

Clipper

Files

Library is rtl

Seealso

ERASE, FERASE(), FERROR(), FILE(), RENAME

FError()

FERROR()

Reports the error status of low-level file functions

Syntax

      FERROR() --> <nErrorCode>

Returns

<nErrorCode> Value of the DOS error last encountered by a low-level file function.

FERROR() Return Values

       Error          Meaning
       -------------- ----------------------------------------------
       0              Successful
       2              File not found
       3              Path not found
       4              Too many files open
       5              Access denied
       6              Invalid handle
       8              Insufficient memory
       15             Invalid drive specified
       19             Attempted to write to a write-protected disk
       21             Drive not ready
       23             Data CRC error
       29             Write fault
       30             Read fault
       32             Sharing violation
       33             Lock Violation

Description

After every low-level file function, this function will return a value that provides additional information on the status of the last low-level file functions’s performance. If the FERROR() function returns a 0, no error was detected. Below is a table of possibles values returned by the FERROR() function.

Examples

      #include "fileio.ch"
      nHandle := FCreate( "temp.txt", FC_NORMAL )
      IF FError() != 0
         ? "Cannot create file, DOS error ", FError()
      ENDIF

Compliance

Clipper

Files

Library is rtl

Seealso

FCLOSE(), FERASE(), FOPEN(), FWRITE()

FErase()

FERASE()

Erase any file from disk by low-level access

Syntax

      FERASE( <cFile> ) --> nSuccess

Arguments

<cFile> Name of file to erase.

Returns

<nSuccess> 0 if successful, -1 if not

Description

This function deletes the file specified in <cFile> from the disk. No extensions are assumed. The drive and path my be included in <cFile>; neither the SET DEFAULT not the SET PATH command controls the performance of this function. If the drive or path is not used, the function will look for the file only on the currently selected directory on the logged drive.

If the function is able to successfully delete the file from the disk, the value of the function will be 0; otherwise a -1 will be returned. If not successfull, additional information may be obtained by calling the FERROR() function.

Note: Any file to be removed by FERASE() must still be closed.

Examples

      #include "fileio.ch"
      IF FErase( "test.txt" ) != F_ERROR
         ? "File successfully erased"
      ELSE
         ? "File can not be deleted"
      ENDIF

Compliance

Clipper

Files

Library is rtl

Seealso

FERROR(), FRENAME()

ERASE

ERASE

Remove a file from disk

Syntax

      ERASE <xcFile>

Arguments

<xcFile> Name of file to remove

Description

This command removes a file from the disk. The use of a drive, directo- ry, and wild-card skeleton operator is allowed for the root of the filename. The file extension is required. The SET DEFAULT and SET PATH commands do not affect this command.

The file must be considered closed by the operating system before it may be deleted.

Examples

      ERASE C:\temp\read.txt

Compliance

Clipper

Seealso

CURDIR(), FILE(), FERASE(), DELETE FILE