xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

FWrite()

Writes data to an open binary file.

Syntax

FWrite( <nFileHandle>, ;
        <cBuffer>    , ;
       [<nBytes>]    , ;
       [<nOffset>]     ) --> nBytesWritten

Arguments

<nFileHandle>
This is a numeric file handle returned from function FOpen() or FCreate().
<cBuffer>
This is a character string to be written to the open file.
<nBytes>
A numeric value specifying the number of bytes to write to the file, beginning with the first character of <cBuffer>. It defaults to Len( <cBuffer> ), i.e. all bytes of <cBuffer>.
<nOffset>
This is a numeric value specifying the number of bytes to skip at the beginning of <cBuffer>. This allows to write data from the middle of a string buffer into a file. The default value is zero. Note that the sum of <nBytes>+<nOffset> must be smaller than or equal Len(<cBuffer>).

Return

The function returns a numeric value which is the number of bytes written to the file. If the return value equals <nBytes>, the operation was succesful. Any value differeing from <nBytes> indicates failure, which can be identified using function FError().

Description

The low-level file function FWrite() writes data provided in form of a character string into an open file. Data is written starting at the current position of the file pointer. The file pointer is advanced to a new position by the number of written bytes.

Info

See also:FClose(), FCreate(), FError(), FOpen(), FRead(), FReadStr(), FWrite()
Category: File functions , Low level file functions
Source:rtl\philes.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example implements the user defined function WriteStream()
// which writes an entire character string into a newly created file

   PROCEDURE Main
      LOCAL aDir   := Directory()
      LOCAL cFiles := ""
      AEval( aDir, {|a| cFiles += a[1] + Chr(13)+Chr(10) } )

      IF .NOT. WriteStream( "Files.txt", cFiles )
         ? "Error writing file", FError()
      ENDIF
   RETURN

   FUNCTION WriteStream( cFile, cStream )
      LOCAL nHandle := FCreate( cFile )

      IF FError() <> 0
         RETURN .F.
      ENDIF

      FWrite( nHandle, cStream, Len(cStream) )
      FClose( nHandle )
   RETURN ( FError() == 0 )

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