User Tools

Site Tools


Read a file using a stream

If you need to read a larger file and process it during the read, you should use a stream for it. This is largely used when reading or writing in the network using internet protocols.

This is a sample how to read a file using a stream:

using System.IO
using System.Text

function ReadFileStream( cFileName as string ) as void
local oFileStream as FileStream
local aBytes as byte[]
local nOffset as int
local nRead as int   
local nBufLen as int
local cBuffer as string
nOffset	:= 0
nBufLen := 10
cBuffer	:= ""  
aBytes := byte[]{ 10 }
oFileStream := FileStream{ cFileName, FileMode.Open, FileAccess.Read }
while ( nRead := oFileStream:Read( aBytes, 0, nBufLen ) ) > 0
  cBuffer := cBuffer + System.Text.Encoding.UTF32:GetString( aBytes )
  nOffset := nOffset + nRead
System.Console.WriteLine( "Read with stream:" + cBuffer )    

You will find more information about the System.IO.Stream class in the MSDN, together with the child classes like the FileStream class. Please pay attention to the correct Encoding!

read_file_stream.txt · Last modified: 2018/01/27 20:44 by wolfgangriedmann