Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StreamReader throws exception when reading not from the 1st char 1

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
566
0
16
US
Colleagues, subject line says it.
Details:
1. Code:
Code:
Do While liEndPos = 0
	If liEndPos > 0 Then
		liEndPos -= 1
	End If

	ReDim laChars(liArrLen)
	loStreamReader = New StreamReader(tcSrcFile)
	loStreamReader.Read(laChars, liStartPos, liArrLen)

	lsReadStr = CompilerServices.Conversions.FromCharArray(laChars)
	
' There can be NULLs at the end of the array, let's clean 'em out
	For I As Int32 = laChars.Length - 1 To 0 Step -1
		If IsNothing(laChars(I)) Or laChars(I) = vbNullChar Then
			ReDim Preserve laChars(I - 1)
		End If
	Next

	liEndPos = InStrRev(lsReadStr, tsEndDelim, CompareMethod.Text)
	My.Computer.FileSystem.WriteAllText(tcOutFile, lsReadStr, True)
Loop

2. MS VS 2019 on-line Help states (ref.
" Read(Char[], Int32, Int32)

Reads a specified maximum of characters from the current stream into a buffer, beginning at the specified index."

3. On the very 1st iteration of the cycle, this Exception was thrown (dimensions in the red ink):

2023_02_24_13_33_StreamReaderErrsOn_BufferSize_kuftdb.jpg


So, the file is twice as long as buffer (Char array, 16 Mb), the starting point (or "index") to read from is 4K. I tried this code with the Char array reduced to 1 Mb, even to 16 Kb - the outcome was the same: this same exception thrown.
What am I doing wrong?
Please review and advise, AHWBGA!


Regards,

Ilya
 
You didn't read the documentation completely...

Microsoft said:
Parameters

buffer Char[]
When this method returns, contains the specified character array with the values between index and (index + count - 1) replaced by the characters read from the current source.

index Int32
The index of buffer at which to begin writing.

count Int32
The maximum number of characters to read.

Returns Int32
The number of characters that have been read, or 0 if at the end of the stream and no data was read. The number will be less than or equal to the count parameter, depending on whether the data is available within the stream.

To summarise, index is the start position in the array, not the file

 
StromgM said:
index is the start position in the array, not the file
Scheise! (Pardon my French! ... Err... German. :) )
So, how does one read from a file by chunks, starting each next chunk on the byte where the previous ended +1?
(I tried to find it in the Help but...)

Regards,

Ilya
 
>starting each next chunk on the byte where the previous ended +1

Well, that's what it does. The following (assuming we have just opened the stream, so we are positioned at the start):

loStreamReader.Read(laChars, 0, liArrLen)

will read liArrLen chars in to the laChars array, and leave the next read position at liArrLen +1

Since just you seem to want to start 4k into the file, just

loStreamReader.Read(laChars, 0, 4096)

to read the first 4096 characters, and then do nothing with them, simply ignore what you have just read from the stream,. Read position will now be at 4097 ...

You CAN play with the underlying BaseStream and use it's Seek method, but you then generally need to ensure the StreamReader and the BaseStream are in sync by using the streamreader's DiscardBufferedData method
 
This did work!
Thank you StrongM! [bow]
The issue's been resolved.


Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top