WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Binary Functions

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Use BinaryRead Like FileRead


Question:

Have large file over 1.5 million text lines, and need to load it using BinaryRead, then I need to read each line sequentially like I would be reading a file (1 line at a time) using FileRead, can anybody help with the code ?

Reason for this request is that FileRead is just too slow.

Answer:

One of these should work. Just insert your code where noted. The second version requires that you have a recent version of WB. 2003G or newer. I don't know at what point filesize becomes a problem. Do you have a lot of RAM?

Binary read version

filename = "c:\test\test.txt"
size = FileSize(filename)
hnd = BinaryAlloc(size)
ret = BinaryRead(hnd,filename)
start = 0

While @TRUE

  found = BinaryIndexEx(hnd,start,@CRLF,@FWDSCAN,0)
  If found == -1
    If start >= size - 1
      Break
    EndIf
    found = size - 1  ; last line does not end in a crlf
  EndIf
  line = BinaryPeekStr(hnd,start,found-start+1)
  Message("read",line)

  ; You would do your stuff here

  start = found + 2
  If start >= size Then Break

EndWhile

BinaryFree(hnd)

Exit

FileGet version

filename = "c:\test\test.txt"
file = FileGet(filename)
file = StrReplace(file,@CRLF,@TAB)
llinecount = ItemCount(file,@TAB)

For ii = 1 To linecount

  line = ItemExtract(ii,file,@TAB)

  ; You would do your stuff here

Next

Exit

Article ID:   W15894
File Created: 2004:03:30:15:41:30
Last Updated: 2004:03:30:15:41:30