Usina BRAdvPL Guide
Sign in
← All topics
FUNCTIONUnder review

FREADSTR()

FReadStr( nHandle, nQtdBytes ) --> cRet

Reads up to a byte limit and returns text.

FREADSTR()AdvPL
01 · OVERVIEW

Overview

FReadStr returns a string unlike FRead’s numeric count. It can stop at Chr(0), EOF or error; it is not a generic replacement for binary content requiring preserved null bytes. The GitHub source demonstrates reading through a writable open mode; this adaptation uses read-only access and captures diagnostics before closure. Returned length depends on file and position rather than Unicode character count. (TOTVS, n.d., ref. 1)

02 · SYNTAX

Syntax

FReadStr( nHandle, nQtdBytes ) --> cRet

Parameters

nHandle
NumericRequired

Valid handle from FOpen/FCreate; zero is valid too.

nQtdBytes
NumericRequired

Requested byte limit.

Return value

Text read up to the limit, null or EOF.

03 · PRACTICAL EXAMPLE

Commented example

#Include "Protheus.ch"
#Include "Fileio.ch"

User Function Ed394()
    Local nHandle := -1
    Local nError := 0
    Local cText
    // Pre-existing fictional file in the test context; shared reading.
    nHandle := FOpen("editorial-test.txt", FO_READ + FO_SHARED)
    If nHandle < 0
        nError := FError()
        ConOut(cValToChar(nError))
    Else
        cText := FReadStr(nHandle, 64)
        nError := FError()
        // Report size without exposing file content.
        ConOut(cValToChar(Len(cText)))
        If nError <> 0
            ConOut(cValToChar(nError))
        EndIf
        If !FClose(nHandle)
            ConOut(cValToChar(FError()))
        EndIf
    EndIf
Return Nil
Expected result

Length up to 64 bytes or a diagnostic; content is not shown. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

BEST PRACTICES
  • Check path, permissions, platform and handle ownership. Close only handles obtained by this routine, including complete-application error handling.
  • Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Related content

REFERENCES
  1. TOTVS. FREADSTR(). TDN. Accessed on 2026-10-26.
  2. EURAI. ADVPL/Funcoes/Arquivos de Disco/FReadStr.PRW. universo_do_desenvolvedor_site, commit a5c83e9490302bdb4a0fc5a9d92c2d569a60773c. Exemplo analisado. Accessed on 2026-10-26.
Status
Under review
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR
0 approved comment(s)

Comments

There are no approved comments yet.

Sign in with Google or Microsoft to comment.

Powered by Usina Docs · Alpha