Click here to Skip to main content
16,016,925 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a code like this to read from text file, but if my text file has no text then my code does not work and shows an error

MY CODE:

VB
Dim sSourceFile, sLine As String
        Dim a As String
        sSourceFile = "E:\hash\WindowsApplication1\WindowsApplication1\bin\a.txt"
        FileOpen(100, sSourceFile, OpenMode.Input, OpenAccess.Read)

        sLine = LineInput(100)
        a = Mid(sLine, 1)
        ListBox1.Items.Add(a)
        While Not EOF(100)
            sLine = LineInput(100)
            a = Mid(sLine, 1)
            ListBox1.Items.Add(a)
        End While
        FileClose(100)
        IP_Server = ListBox1.Items(0)
        server = ListBox1.Items(1)
        DB = ListBox1.Items(2)
        User_Server = ListBox1.Items(3)
        Pass = ListBox1.Items(4)




my error if text file is empty:

Input past end of file.
this error show for line " sLine = LineInput(100)"


tnx for yr help
Posted
Updated 22-Nov-11 21:12pm
v3

1 solution

Why you don't ask what to do if a file is two times shorted than expected? What to do it it contains some garbage? Effectively, this is exactly the same as empty file.

You can ask yourself why do you allow to read zero bytes in the file in first place and fix it, but this is not the most important thing.

You should always expect that any file contains anything unexpected. You can expect that a file is inaccessible. In this way, you don't need to check any special condition like if the file is empty or something like that. You cannot predict all possible error conditions with file. Anyone could get to the file and mangle or truncate any number of bytes. What you see is the legitimate exception. Catch the exception, get exception information (type, message), for know exception types wrap it in some reasonable explanation text and present to the user.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900