Unable to read beyond the end of the stream

G

Guest

I have the code below:
Private Sub cmdFill_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdFill.Click
Dim Employee As UploadFile, Count As Integer, Temp As String
FileNum = FreeFile()
FileOpen(FileNum, TextBox1.Text, OpenMode.Random, , , Len(Employee))
Count = 1
lstView.Items.Clear()
Do While Not EOF(FileNum)
FileGet(FileNum, Employee, Count)
Temp = Str(Employee.UploadID) '+ " " + Employee.Name + " " +
Employee.Surname
lstView.Items.Add(Temp)
Count = Count + 1
Loop
FileClose(FileNum)
End Sub

Where "UploadFile" is a structure with 3 fields and "TextBox1.Text =
test.txt". Unfortunately at "FIleGet" I get the error message:
'System.IO.EndOfStreamException' Unable to read beyond the end of the stream.

Frustrated please help.
 
H

Herfried K. Wagner [MVP]

Zak Milas said:
I have the code below:
Private Sub cmdFill_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdFill.Click
Dim Employee As UploadFile, Count As Integer, Temp As String
FileNum = FreeFile()
FileOpen(FileNum, TextBox1.Text, OpenMode.Random, , ,
Len(Employee))
Count = 1
lstView.Items.Clear()
Do While Not EOF(FileNum)
FileGet(FileNum, Employee, Count)
Temp = Str(Employee.UploadID) '+ " " + Employee.Name + " " +
Employee.Surname
lstView.Items.Add(Temp)
Count = Count + 1
Loop
FileClose(FileNum)
End Sub

Where "UploadFile" is a structure with 3 fields and "TextBox1.Text =
test.txt". Unfortunately at "FIleGet" I get the error message:
'System.IO.EndOfStreamException' Unable to read beyond the end of the
stream.

Are you sure the file is long enough, which means, that the whole last
record can be read from the file?
 
C

Cor Ligthert

Herfried,
Are you sure the file is long enough, which means, that the whole last
record can be read from the file?
Not offended however what do you mean by this?

Cor
 
H

Herfried K. Wagner [MVP]

Cor,

Cor Ligthert said:
Not offended however what do you mean by this?

Let's assume that the ASCII art below represents the file. Structure
instances are separated by "+" characters:

|----+----+----+--|

In the sample above, the last structure isn't saved completely, because for
some reason the file's length was reduced. So, when attempting to read the
last structure instance, an error would occur.
 
G

Guest

Yes the content of the file for testing purposes is "1", that is the only
thing in the file. Of course once i get it work there is going to a lot more
info then just "1".
 
H

Herfried K. Wagner [MVP]

Zak Milas said:
Yes the content of the file for testing purposes is "1", that is the only
thing in the file. Of course once i get it work there is going to a lot
more
info then just "1".

Well, if the file only contains "1" and you are attempting to read a whole
structure instance with multiple members, this will fail.
 
G

Guest

Below is my Structure, I have commented everything except the first one.
Structure UploadFile
Dim UploadID As Integer
'Dim SerialNumber As String
'Dim CurrentDate As Date
'Dim SupportStaff As String
'Dim ClientEmail As String
'Dim FileName As String
'Dim FileSize As Integer
'Dim EmailType As String
End Structure

therefore its reading only one member in the structure.
 
H

Herfried K. Wagner [MVP]

Zak Milas said:
Below is my Structure, I have commented everything except the first one.
Structure UploadFile
Dim UploadID As Integer
'Dim SerialNumber As String
'Dim CurrentDate As Date
'Dim SupportStaff As String
'Dim ClientEmail As String
'Dim FileName As String
'Dim FileSize As Integer
'Dim EmailType As String
End Structure

therefore its reading only one member in the structure.


Did you use 'FilePut' for a file that was opened in binary mode
('FileOpen(..., ..., OpenMode.Binary)') when writing the file?
 
G

Guest

Actually the file was not opened in binary mode. I opened it as Random. After
I had used the FilePut(FileNum, Employee, Count) command, what ever was in my
text file was removed.
 
G

Guest

Hello Thank you for your input. i found out what the problem was. It is the
content of the text file needed a space and a new line after the last entry.
Thank you once again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top