Click or drag to resize

FReadLine Function

X#
Read a line from an open file.

Namespace:  XSharp.Core
Assembly:  XSharp.Core (in XSharp.Core.dll) Version: 2.19
Syntax
 FUNCTION FReadLine(
	ptrHandle AS IntPtr,
	nMax AS DWORD
) AS STRING
Request Example View Source

Parameters

ptrHandle
Type: IntPtr
The handle of the file to read from.
nMax
Type: DWord
The maximum number of characters to read per line. FReadLine() will read until a hard carriage return (Chr(13)) is reached, end-of-file is encountered, or nMax characters are read.
The default value for nMax is 256.

Return Value

Type: String
The line read. When the end-of-file is reached, FReadLine() returns a NULL_STRING and FError() is set to 257.
Remarks
This function is the same as FGets(). Both functions are assumed to handle raw binary data and are not dependent upon the status of SetAnsi(). FReadText() and FRead4(), on the other hand, are dependent upon SetAnsi().
Remarks
Tip Tip
The low level File IO functions in the X# runtime are using .Net filestreams in the background.
That means that the file handles returned by FOpen() and FCreate() are not 'normal' file handles, but unique identifiers that are used to find the underlying stream object in a collection of streams in the runtime.
That also means that you can't use file handles for functions such as FRead() and FWrite() that were not created in the X# runtime.
If you want to access the underlying FileStream, then you should call the function FGetStream(IntPtr)
Examples
This example uses FReadLine() to read an entire file without specifying a value for nMax:
X#
1ptrHandle := FOpen2("docs.txt", FO_READ)
2IF ptrHandle != F_ERROR
3    DO WHILE !FEOF(ptrHandle)
4        ? FReadLine(ptrHandle)
5    ENDDO
6ENDIF
7FClose(ptrHandle)
The following example provides a utility that displays and counts all occurrences of a string within a file:
X#
 1FUNCTION Grep(cSearch, cFile AS STRING) ;
 2        AS DWORD PASCAL
 3    LOCAL handle AS PTR
 4    LOCAL Count AS DWORD
 5    LOCAL Line AS STRING
 6    Line := " "
 7    handle := FOpen2(file, FO_READ)
 8    cSearch := Upper(cSearch)
 9    DO WHILE !FEOF(handle)
10        line := Upper(FReadLine(handle))
11        IF InStr(cSearch, line)
12            ? line
13        Count += 1
14        ENDIF
15    ENDDO
16    RETURN Count
See Also