Repository navigation
Modern Fs TextReader
Paul Squires edited this page Oct 24, 2026
·
4 revisions
Home › Filesystem › File Streams › TextReader
Holds a reader that decodes lines and text from any Stream, in UTF-8 or UTF-16.
type TextReader
declare constructor( byref inner as Stream, byval mode as TextErrorMode = TEXT_REPLACE )
declare constructor( byref inner as Stream, byval enc as TextEncoding, byval mode as TextErrorMode = TEXT_REPLACE )
declare constructor( byref inner as Stream, byref opts as const TextDetect, byval mode as TextErrorMode = TEXT_REPLACE )
ReadLine( line ) / ReadToEnd( s ) / Encoding( ) / Detected( )
end type- A wrapper does not own what it wraps: it keeps a pointer to the stream it was given, which must outlive it. It cannot be copied.
- Decodes by ReadAllText's rules: a UTF-8, UTF-16LE or UTF-16BE byte-order mark is honoured and dropped; no mark means UTF-8; invalid UTF-8 becomes U+FFFD, or fails with FS_INVALID_DATA under TEXT_STRICT. A file read with ReadAllText and through a TextReader gives the same text.
- A multi-byte sequence split between two reads of the inner stream is joined before it is decoded.
- With an encoding, it reads as ReadAllText( p, enc ) does: that encoding, whatever the data starts with, and only a byte-order mark that matches it is dropped. See Choosing An Encoding.
- With a TextDetect, it reads the first opts.PrefixBytes bytes (64 KiB) before its first line and picks the encoding from them: a byte-order mark, else UTF-8 if they are valid UTF-8, else opts.Fallback. Detected( ) returns the encoding it picked.
- Without an encoding, a UTF-32 byte-order mark at the start fails the first ReadLine or ReadToEnd with FS_UNSUPPORTED. With an encoding, the encoding is believed.
- Not a Stream: it produces text, not bytes.
#import "std/fs"
using fs
dim as MemoryStream m = MemoryStream( "alpha" & chr( 10 ) & "beta" )
dim as TextReader tr = TextReader( m )
dim as ustring ln
while tr.ReadLine( ln ).ValueOr( false )
print ln
wend
'' alpha
'' betaLibrary: Filesystem Library Category: File Streams Documented under: Streams
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing