User Tools

Site Tools


read_file_stream

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
read_file_stream [2018/01/27 20:14] – created wolfgangriedmannread_file_stream [2018/01/27 20:44] (current) wolfgangriedmann
Line 1: Line 1:
 ====== Read a file using a stream ====== ====== 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:
 +<code>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
 +end
 +System.Console.WriteLine( "Read with stream:" + cBuffer )    
 +
 +return</code>
 +
 +You will find more information about the [[https://docs.microsoft.com/en-us/dotnet/api/system.io.stream|System.IO.Stream]] class in the MSDN, together with the child classes like the FileStream class.
 +Please pay attention to the correct [[encodings|Encoding]]!
  
read_file_stream.txt · Last modified: 2018/01/27 20:44 by wolfgangriedmann