xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

HB_FUse()

Opens or closes a text file in a text file area.

Syntax

HB_FUse( [<cTextfile>], [<nMode>] ) --> nFilehandle

Arguments

<cTextfile>
This is a character string holding the name of the text file to open. It must include path and file extension. If the path is omitted from <cTextfile>, the file is searched in the current directory.

If no parameter is passed, the text file open in the currently selected text file area is closed.

<nMode>
A numeric value specifying the open mode and access rights for the file. #define constants from the FILEIO.CH file can be used for <nMode> as listed in the table below:

File open modes
ConstantValueDescription
FO_READ *)0Open file for reading
FO_WRITE1Open file for writing
FO_READWRITE2Open file for reading and writing

Constants that define the access or file sharing rights can be added to an FO_* constant. They specify how file access is granted to other applications in a network environment.

File sharing modes
ConstantValueDescription
FO_COMPAT *)0Compatibility mode
FO_EXCLUSIVE16Exclusive use
FO_DENYWRITE32Prevent other applications from writing
FO_DENYREAD48Prevent other applications from reading
FO_DENYNONE64Allow others to read or write
FO_SHARED64Same as FO_DENYNONE

Return

The function returns a numeric file handle when the text file is successfully open, or zero on failure.

Description

HB_FUse() opens an existing text file named <cTextfile> in the currently selected text file area. All text file functions operate only in the currently selected text file area. Call HB_FSelect() to change a text file area for opening multiple text files.

The file open in the currently selected text file area is closed when HB_FUse() is called without parameters.

Info

See also:FOpen(), FSeek(), HB_FLastRec(), HB_FGoto(), HB_FReadLN(), HB_FSkip(), HB_FSelect()
Category: File functions , Text file functions , xHarbour extensions
Source:misc\hb_f.c
LIB:libmisc.lib

Example

// The example implements a simple text file viewer using a
// TBrowse object and text file functions.

   #include "Common.ch"
   #include "Inkey.ch"
   #include "TBrowse.ch"

   PROCEDURE Main( cFile )
      LOCAL nFile, oTBrowse

      SET CURSOR OFF
      nFile := HB_FUse( cFile )

      IF nFile < 1
         ? "File not found", cFile
         QUIT
      ENDIF

      oTBrowse := TxtBrowse()

      RunTxtBrowse( oTBrowse )

      HB_FUse()
   RETURN

   PROCEDURE RunTxtBrowse( oTBrowse )
      LOCAL nKey, nLen, lRun := .T.

      DO WHILE lRun
         oTBrowse:forceStable()
         nKey := Inkey(0)

         SWITCH nKey
         CASE K_LEFT
            IF oTBrowse:cargo > 1
               oTBrowse:cargo --
               oTBrowse:refreshAll()
            ENDIF
            EXIT

         CASE K_RIGHT
            oTBrowse:cargo ++
            oTBrowse:refreshAll()
            EXIT

         CASE K_HOME
            IF oTBrowse:cargo > 1
               oTBrowse:cargo := 1
               oTBrowse:refreshAll()
            ENDIF
            EXIT

         CASE K_END
            nLen := Len( HB_FReadLN() )
            IF nLen-oTBrowse:cargo+1 > 72
               oTBrowse:cargo := nLen - 72 + 1
               oTBrowse:refreshAll()
            ENDIF
            EXIT

         CASE K_CTRL_HOME
            EXIT

         CASE K_CTRL_END
            EXIT

         DEFAULT
            IF oTBrowse:applyKey( nKey ) == TBR_EXIT
               lRun := .F.
            ENDIF
         END
      ENDDO
   RETURN


   FUNCTION TxTBrowse( nT, nL, nB, nR )
      LOCAL oTBrowse, oTBCol1, oTBCol2

      DEFAULT nT TO 0, ;
              nL TO 0, ;
              nB TO MaxRow(), ;
              nR TO MaxCol()

      oTBrowse := TBrowseNew( nT, nL, nB, nR )
      oTBrowse:cargo := 1

      oTBCol1  := TBColumnNew( " ",  ;
                              {|| Padr(HB_FRecno(),5)+":" } )

      oTBCol2  := TBColumnNew( " ",  ;
                              {|| Padr( SubStr( HB_FReadLN(), oTBrowse:cargo), 72 ) } )

      WITH OBJECT oTBrowse
         :addColumn( oTbCol1 )
         :addColumn( oTbCol2 )

         :goTopBlock    := {|| HB_FGotop() }
         :goBottomBlock := {|| HB_FGoBottom() }
         :skipBlock     := {|n| TxtSkipper(n) }
         :colPos        := 2
      END

   RETURN oTBrowse


   FUNCTION TxtSkipper( nRequest )
      LOCAL nSkip := 0

      DO CASE
      CASE nRequest == 0
      CASE nRequest < 0

         DO WHILE nSkip > nRequest .AND. HB_FRecno() > 1
            HB_FSkip(-1)
            nSkip --
         ENDDO

      CASE nRequest > 0
         DO WHILE nSkip < nRequest
            HB_FSkip(1)
            IF HB_FEof()
               EXIT
            ENDIF
            nSkip ++
         ENDDO
      ENDCASE

   RETURN nSkip

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe