Detecting End of File

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am importing a file using the following code but I am having difficulty
determining the end of the file, Currently I just create a very large array &
hope it fits.

ReDim a(0 To 100000)
inputfile = "C:\Temp\Remittance.dat"
Open inputfile For Binary Access Read As #2
Get #2, , a
Close #2
For Cnt = 0 To UBound(a()) '100000
Str = Str & Chr(a(Cnt))
Next

Amiga1200
 
The FileLen function will return the size of the file in bytes. Use that to
determine what the upper limit of your array needs to be.
 
There's no need to loop through the array to assemble the string one
character at a time: you can "Get" straight from the file into a
presized string variable:

Dim strContents As String
Dim lngFN As Long

Const FName = "C:\TEMP\60kAsTruncated.txt"

lngFN = FreeFile()
Open FName For Binary Access Read As #lngFN
strContents = Space(LOF(lngFN)) 'allocate string
Get #lngFN, , strContents
Close #lngFN

Simpler still, use Input() instead of Get, which means you don't have to
pre-size the string:

strContents = Input(LOF(lngFN), #lngFN)
 

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

Back
Top