# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  VB6 - Huge (>2GB) File I/O Class

## dilettante

I swear I did a search or two first but I hadn't seen this here.

HugeBinaryFile.cls is a VB6 Class that is based on an old MS KB article for VB4.

It works a bit like using Get#/Put# with Byte arrays, and supports absolute seeks to a byte position using a Currency value from 0 to far beyond the usual 2GB limit.  It can also do seeks relative to the current file position accepting a signed Long value, and seek to EOF for appending.  Its FileLen property returns the open file's length in bytes as a Currency value.

Currency was used for convenience since huge files need a 64-bit position value.  Since Currency values have an implied decimal point the class does scaling so that you can use 1 to mean 1 (1.0000) instead of using 0.0001 to mean byte 1.

If you find this imperfect you can always modify the Class to accept and return a pair of unsigned Long values instead.  In the end these can get pretty clumsy to work with though.

I did a certain amount of testing, but I won't claim this code is bug-free.


The Class is provided here bundled in a small demo project.  To add it to your own projects simply copy the .cls file into your project folder and Add|File... to make it part of your project.


It would be fairly easy to create another Class that wraps this one for doing text I/O.

*Look at the posts below for my HugeTextFile class.*

----------


## Chris001

Here's another example. Somebody wrote this for me a few years ago.

Put it in a module.



```
Option Explicit

Const MOVEFILE_REPLACE_EXISTING = &H1
Const FILE_ATTRIBUTE_TEMPORARY = &H100
Const FILE_BEGIN = 0
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const CREATE_NEW = 1
Const OPEN_EXISTING = 3
Const OPEN_ALLWAYS = 4
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function SetEndOfFile Lib "kernel32" (ByVal hFile As Long) As Long

Public File_Num As Long
Public File_Len As Currency

Public Sub API_OpenFile(ByVal FileName As String, ByRef FileNumber As Long, ByRef FileSize As Currency)
Dim FileH As Long
Dim Ret As Long
On Error Resume Next
FileH = CreateFile(FileName, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_ALLWAYS, 0, 0)
If Err.Number > 0 Then
    Err.Clear
    FileNumber = -1
Else
    FileNumber = FileH
    Ret = SetFilePointer(FileH, 0, 0, FILE_BEGIN)
    API_FileSize FileH, FileSize
End If
On Error GoTo 0
End Sub

Public Sub API_FileSize(ByVal FileNumber As Long, ByRef FileSize As Currency)
    Dim FileSizeL As Long
    Dim FileSizeH As Long
    FileSizeH = 0
    FileSizeL = GetFileSize(FileNumber, FileSizeH)
    Long2Size FileSizeL, FileSizeH, FileSize
End Sub

Public Sub API_ReadFile(ByVal FileNumber As Long, ByVal Position As Currency, ByRef BlockSize As Long, ByRef Data() As Byte)
Dim PosL As Long
Dim PosH As Long
Dim SizeRead As Long
Dim Ret As Long
Size2Long Position, PosL, PosH
Ret = SetFilePointer(FileNumber, PosL, PosH, FILE_BEGIN)
Ret = ReadFile(FileNumber, Data(0), BlockSize, SizeRead, 0&)
BlockSize = SizeRead
End Sub

Public Sub API_CloseFile(ByVal FileNumber As Long)
Dim Ret As Long
Ret = CloseHandle(FileNumber)
End Sub

Public Sub API_WriteFile(ByVal FileNumber As Long, ByVal Position As Currency, ByRef BlockSize As Long, ByRef Data() As Byte)
Dim PosL As Long
Dim PosH As Long
Dim SizeWrit As Long
Dim Ret As Long
Size2Long Position - 1, PosL, PosH
Ret = SetFilePointer(FileNumber, PosL, PosH, FILE_BEGIN)
Ret = WriteFile(FileNumber, Data(0), BlockSize, SizeWrit, 0&)
BlockSize = SizeWrit
End Sub

Private Sub Size2Long(ByVal FileSize As Currency, ByRef LongLow As Long, ByRef LongHigh As Long)
'&HFFFFFFFF unsigned = 4294967295
Dim Cutoff As Currency
Cutoff = 2147483647
Cutoff = Cutoff + 2147483647
Cutoff = Cutoff + 1 ' now we hold the value of 4294967295 and not -1
LongHigh = 0
Do Until FileSize < Cutoff
    LongHigh = LongHigh + 1
    FileSize = FileSize - Cutoff
Loop
If FileSize > 2147483647 Then
    LongLow = -CLng(Cutoff - (FileSize - 1))
Else
    LongLow = CLng(FileSize)
End If
End Sub

Private Sub Long2Size(ByVal LongLow As Long, ByVal LongHigh As Long, ByRef FileSize As Currency)
'&HFFFFFFFF unsigned = 4294967295
Dim Cutoff As Currency
Cutoff = 2147483647
Cutoff = Cutoff + 2147483647
Cutoff = Cutoff + 1 ' now we hold the value of 4294967295 and not -1
FileSize = Cutoff * LongHigh
If LongLow < 0 Then
    FileSize = FileSize + (Cutoff + (LongLow + 1))
Else
    FileSize = FileSize + LongLow
End If
End Sub
```

----------


## DigiRev

> Here's another example. Somebody wrote this for me a few years ago.
> 
> Put it in a module.
> 
> 
> 
> ```
> Option Explicit
> 
> ...


I remember that.  :Wink:  I think I did, but I actually got it from another site. I needed it for large files > 2 and 4GB for my Winsock downloader.

Wish I knew this class file existed though.

----------


## halsboss

A couple of queries from a beginner re setfilepointer ... 

1. is the pointer 0-based or 1-based for the 1st byte of a file ?  
ie by way of example, what value do I give setfilepointer to then read the 2nd byte of a file ... 1 or 2 ?

2. But... In the 2nd set of code above from DigiRev, for a READ it does setfilepointer to (Position) yet for a WRITE it does setfilepointer to (Position - 1) first ... which seems to me to overwrite starting on the top of the previous byte... that isn't how GET/PUT work, is it ?
eg if position 2 were specified, it would do a setfilepointer to position 1 and then start writing on the top of position 1 onward ... is that a bug ?

Cheerio

----------


## Chris001

1) 0-based. First byte = position 0, second byte = position 1

2) It should not be (Position - 1). I used that code for an application where I always had to write to (Position - 1) and to make it easier for myself I added (- 1) to the API_WriteFile sub. You need to remove that.

----------


## halsboss

Thanks, I ended up changing it a bit for my own purposes... should I post it or leave it alone ?

----------


## Chris001

If you think it's useful for other people, then post it  :Smilie:

----------


## halsboss

OK here it is.

----------


## Henilein

Dear members,

I tried your latest class for huge files, but I can't find 
SetFilePointerEx in my kernel32.dll.

I use Windows NT, SP2. :Roll Eyes (Sarcastic): 

Can you give me a hint ?

best regards henilein.

----------


## halsboss

Sorry mate, http://msdn.microsoft.com/en-us/libr...42(VS.85).aspx says win2k and greater for the EX extended functions.

----------


## vbguy222

A quick note for anyone that might come through here and want to use the code posted above. The API_FileSize method incorrectly reports 0-byte file sizes as being 4294967295 bytes in length. To fix this, modify the last line in the method from


```
Long2Size FileSizeL, FileSizeH, FileSize
```

to this



```
If FileSizeL = -1 Then
   FileSize = 0
Else
   Long2Size FileSizeL, FileSizeH, FileSize
End If
```

Cheers.

----------


## dilettante

I also have a HugeTextFile class you can use for processing text files.

It works fine for both "normal" sized files and "huge" ones.  It also processes both ANSI and Unicode files (Windows "Unicode" i.e. UTF-16LE), with either CRLF or LF line delimiters..  There are ReadLine, WriteLine, and SkipLines methods, a Rewind, etc. as well as an EOF property, LineCount property reflecting current "progress", and so forth.

Even for moderately-sized files the performance will probably beat the memory-hungry "split-de-split" technique people often clumsily use to read a file in one gulp and create an array of text lines.  If the files are bigger you may want to specify larger buffers when you call the OpenFile method.


The demo project HugeTextDemo uses Timers to keep the UI responsive but feel free to use straight-line looping through entire files in your programs.  That is an entirely separate matter from the HugeTextFile class itself.

The attachment also contains a secondary project MakeFile you should run first to create "old.txt" that HugeTextFile needs.

----------


## dilettante

*Regarding PMs*

Thanks, but I can't answer questions about the code the thread hijackers posted above.  Feel free to PM them directly since they never established their own threads.

If you have questions about the code code samples I attached here ask in the thread.  PMs are sort of clunky for that.

----------


## Jonney

> I swear I did a search or two first but I hadn't seen this here.
> 
> HugeBinaryFile.cls is a VB6 Class that is based on an old MS KB article for VB4.
> 
> It works a bit like using Get#/Put# with Byte arrays, and supports absolute seeks to a byte position using a Currency value from 0 to far beyond the usual 2GB limit.  It can also do seeks relative to the current file position accepting a signed Long value, and seek to EOF for appending.  Its FileLen property returns the open file's length in bytes as a Currency value.
> 
> Currency was used for convenience since huge files need a 64-bit position value.  Since Currency values have an implied decimal point the class does scaling so that you can use 1 to mean 1 (1.0000) instead of using 0.0001 to mean byte 1.
> 
> If you find this imperfect you can always modify the Class to accept and return a pair of unsigned Long values instead.  In the end these can get pretty clumsy to work with though.
> ...


' a small bug in Boundary
'For Unicode support,you should use W-Type API


```

Private Declare Function CreateFileW Lib "kernel32" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function lstrlenW Lib "kernel32" (ByVal psString As Any) As Long
Private Declare Function LCMapStringW Lib "kernel32" (ByVal Locale As Long, ByVal dwMapFlags As Long, ByVal lpSrcStr As Long, ByVal cchSrc As Long, ByVal lpDestStr As Long, ByVal cchDest As Long) As Long

Private Declare Function CreateFile Lib "KERNEL32.dll" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByRef lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function LCMapString Lib "KERNEL32.dll" Alias "LCMapStringA" (ByVal Locale As Long, ByVal dwMapFlags As Long, ByVal lpSrcStr As String, ByVal cchSrc As Long, ByVal lpDestStr As String, ByVal cchDest As Long) As Long
Private Declare Function lstrlen Lib "KERNEL32.dll" Alias "lstrlenA" (ByVal lpString As String) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long

Private Const INVALID_HANDLE_VALUE = -1

Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000

Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2

Private Const CREATE_NEW = 1
Private Const CREATE_ALWAYS = 2
Private Const OPEN_EXISTING = 3
Private Const OPEN_ALWAYS = 4
Private Const TRUNCATE_EXISTING = 5

Private Const FILE_ATTRIBUTE_NORMAL = &H80

Private Const FILE_BEGIN = 0
Private Const FILE_CURRENT = 1
Private Const FILE_END = 2

Public Function ReadBytes(ByRef Buffer() As Byte) As Long
   
   RaiseErrorIfClosed
  
   If ReadFile(hFile, _
      Buffer(LBound(Buffer)), _
      UBound(Buffer) - LBound(Buffer) + 1, _
      ReadBytes, _
      0) Then

      If ReadBytes = 0 Then
         fEOF = True
      ElseIf ReadBytes < UBound(Buffer) - LBound(Buffer) + 1 Then 
         ReDim Preserve Buffer(ReadBytes - 1)
      End If
   Else
      RaiseError HBF_READ_FAILURE
   End If
End Function
```

----------


## dilettante

Since this Class does not deal with text at all, there is no need for the W entrypoints.

However if you want to use Unicode _file names_ then you have a valid point, and CreateFile needs to be CreateFileW with a few other minor changes to its signature and call.

I have no idea why you are using LCMapString though, except perhaps as a fancy version of StrConv?


The nice thing about having the source is that you can hack away and make any customization you want though.  If there really is a bug perhaps you can explain it in more detail?

----------


## Jonney

> Since this Class does not deal with text at all, there is no need for the W entrypoints.
> 
> However if you want to use Unicode _file names_ then you have a valid point, and CreateFile needs to be CreateFileW with a few other minor changes to its signature and call.


Use CreateFileW in your OpenFile function.





> I have no idea why you are using LCMapString though, except perhaps as a fancy version of StrConv?


I just copy all APIs from my another project which deal with Unicode text file. LCMAPStringW for GB2GBK conversion.


```
Public Function GB2GBK(ByVal CSstring As String) As String

  Dim lLen As Long
  Dim CTString As String

    On Error GoTo ToExit

    If CSstring = vbNullString Then Exit Function
    If m_bisNT Then
        lLen = lstrlenW(StrPtr(CSstring))
        CTString = Space$(lLen)
        LCMapStringW &H804, &H4000000, StrPtr(CSstring), lLen, StrPtr(CTString), lLen * 2
        GB2GBK = CTString
      Else
        lLen = lstrlen(CSstring)
        CTString = Space$(lLen)
        LCMapString &H804, &H4000000, CSstring, lLen, CTString, lLen
        GB2GBK = CTString
    End If

Exit Function

ToExit:
    GB2GBK = vbNullString

End Function
```




> The nice thing about having the source is that you can hack away and make any customization you want though.  If there really is a bug perhaps you can explain it in more detail?


Public Function ReadBytes(ByRef Buffer() As Byte) As Long

   RaiseErrorIfClosed

   If ReadFile(hFile, _
      Buffer(LBound(Buffer)), _
*UBound(Buffer) - LBound(Buffer) + 1*, _
      ReadBytes, _
      0) Then

      If ReadBytes = 0 Then
         fEOF = True
*ElseIf ReadBytes < UBound(Buffer) - LBound(Buffer) + 1 Then 
         ReDim Preserve Buffer(ReadBytes - 1)*
      End If
   Else
      RaiseError HBF_READ_FAILURE
   End If
End Function

----------


## dilettante

I guess I see what you are getting at.

Instead of relying on the return value to know how many bytes were read you want to truncate the user's buffer (byte array).  I had chosen not to do that intentionally.  For example the buffer might not be a dynamic array at all, so your change would crash the program.  It also adds a bit of extra cost to the read operation.

There is a reason why this is a function returning the length read.

Even if you did want to do this, you should preserve the user's buffer LBound when redimensioning it.  One more reason to just let the caller do this costly extra step if it is desired.

----------


## SAGremlin

> I remember that.  I think I did, but I actually got it from another site. I needed it for large files > 2 and 4GB for my Winsock downloader.


Lol .. It's quite strange to see this thread... Considering that I'm the Original Author of the posted Code.. (API_OpenFile, API_FileSize, API_ReadFile, API_WriteFile).. My original article 'Getting Past the 2 Gb File Limit' giving full details about how the modules works and why it's needed was done in Jan of 2007 (Long before this thread started).

But it's so nice to see my work has gotten around, and i figured i'd just let everyone know the original source...  :Smilie: 

 :Alien Frog:

----------


## dilettante

> Lol .. It's quite strange to see this thread... Considering that I'm the Original Author of the posted Code.. (API_OpenFile, API_FileSize, API_ReadFile, API_WriteFile).. My original article 'Getting Past the 2 Gb File Limit' giving full details about how the modules works and why it's needed was done in Jan of 2007 (Long before this thread started).
> 
> But it's so nice to see my work has gotten around, and i figured i'd just let everyone know the original source...


You must be talking about the thread hijackers, because my code was 100% original work.  As much as anything so simple can be original anyway.  Earlier versions of the *code this thread is about* date back a lot further than 2007.

----------


## SAGremlin

> You must be talking about the thread hijackers, because my code was 100% original work.  As much as anything so simple can be original anyway.  Earlier versions of the *code this thread is about* date back a lot further than 2007.


No .. not your Class.. If you noted my quote... The code posted by *Chris001* and referenced by *DigiRev* was taken directly from the article that I wrote (which is long before this thread started...) 

Also *DigiRev* say's '_but I actually got it from another site_', so i figured i'll simply put up a link to the original source of his.. (also considering, Codeguru is a sister site to VBForums, he could have found the original source very easily...)

----------


## dilettante

Well that's true enough, what he posted does look like plagiarism.

----------


## roberto9911

Hi, I just tried the first solution because i need to read random a huge file, but when I arrive beside the 2147483647 bytes I've got an overflow calling the readfile api:  it's because the readfile param are declared as long.
Here my code :  someone can help me?

Many thanks in advance!



```

Public Sub ReadFile_2GB(xfilename As String, xlenFile As Double, xlenRec As Double, xStartRecord As Double, xRec As String, xFineFile As Boolean)
' ATTENTION record 1 is with DStartRecord = 0, the second has DStartRecord = 1...
' take care how you call this function!!!

   Dim Temp As Variant
   Dim i As Long
   Dim Ilen As Double
   
   '----xlenRec = record len + CR/FL
   
   If bFirstTime Then
      F.SeekAbsolute 0, 0 'STARTING FROM POSITION 0 OF THE FILE  (Seeks 2 bytes (0*2^32 + 2) = 1 character.
      '----F.SeekRelative -2       ' Seeks forward 1 character (inizio file)
      bFirstTime = False
   Else
      If xlenRec * DStartRecord < xlenFile Then
         'test!!!!
         If xlenRec * DStartRecord <= 2147483647 Then
            F.SeekAbsolute 0, xlenRec * DStartRecord  '  overflow with value = 2.147.483.647! because param is a LONG!!!!)
         Else
            'F.SeekRelative (xlenRec * DStartRecord) ' overflow with value = 2.147.483.647! because param is a LONG!!!!)
            F.SeekAbsolute 1, (xlenRec * DStartRecord) - 2147483647 ' overflow with value = 2.147.483.647! because param is a LONG!!!!)
         End If
      Else
         bEndFile = True
      End If

   End If
   
   Temp = F.ReadBytes(xlenRec)

   Ilen = Len(Temp)
   xRec = ""
   
   'from unicode  to ASCII
   For i = LBound(Temp) To UBound(Temp)
       xRec = xRec & ChrW(Temp(i))
   Next
   
   xStartRecord = xStartRecord + 1
   
End Sub
```

----------


## dilettante

I'm not sure what code you're using.  This thread is about the code in attachments to posts #1 and #12.  The rest are hitchikers here.

SeekAbsolute from post#1 only takes one argument so your:


```
F.SeekAbsolute 0, xlenRec * DStartRecord
```

is incorrect since it is trying to pass two values.

The Position argument is Currency, not Long.  Your problem is that you have used a Long expression.

However SeekRelative _does_ take a Long argument, because that's all Windows supports for relative seeks.

----------


## roberto9911

Yes Dilettante, you are right, I kept another source from somewhere.  Thanks , now I will try to transform the buffer in single records

Kind regards

----------


## brandoncampbell

There is an issue that someone may be able to answer. I can open a log file that is being written to by another process with any other means except for this one. Is there a way to open a file in use, using this class?

----------


## dilettante

Are you using the text file I/O class?  If so, have you tried opening the file passing HTF_OPEN_EXISTING and HTF_SHARE_READ values?

If you are using the binary I/O class you should be able to accomplish the same thing by dealing with the sharing flags and such when opening the file.  The other class should work better for you though since most log files are CRLF-delimited text streams on Windows.

----------


## brandoncampbell

Yes I am using the txt file class and yes crashes the application/vb.

looks similar to this.

    Dim strLine as string
    Set htfIn = New HugeTextFile
    htfIn.OpenFile "C:\Windows\System32\LogFiles\Firewall\pfirewall.log", HTF_ACCESS_READ, HTF_OPEN_EXISTING, HTF_SHARE_READ , BufferSize:=256& * 1024&

    With htfIn
        Do Until .EOF
            strLine = .ReadLine()
        Loop
        .CloseFile
    end with

----------


## brandoncampbell

Yes I am using the txt file class and yes crashes the application/vb.

looks similar to this.

    Dim strLine as string
    Set htfIn = New HugeTextFile
    htfIn.OpenFile "C:\Windows\System32\LogFiles\Firewall\pfirewall.log", HTF_ACCESS_READ, HTF_OPEN_EXISTING, HTF_SHARE_READ , BufferSize:=256& * 1024&

    With htfIn
        Do Until .EOF
            strLine = .ReadLine()
        Loop
        .CloseFile
    end with

----------


## dilettante

What do you mean by "crashes the program" though?

What error number and message do you see?  What gets logged in the Application Event Log?

----------


## brandoncampbell

vb6 crashes with Exception code: 0xc0000005 if I compile the application it hangs and never does anything forcing me to kill the application. No error msg/number. It's relatively a small file at this point so it should hang at all unless there is a problem opening the file. Before it would crash vb but would give an error that the file was in use before I changed the HTF_OPEN_EXISTING, HTF_SHARE_READ

edit:

so if I do this..

With htfIn
Do Until .EOF
strLine = .ReadLine()
*if .eof then
.CloseFile
end if*
Loop
.CloseFile
end with

then i get  run time error 45602 error opening file. error 32

----------


## dilettante

The error 45602 reports system error 32 as the reason.  The system error 32 is ERROR_SHARING_VIOLATION ("The process cannot access the file because it is being used by another process.").  I have no idea how the code you show can get this, since I don't see an open, just two CloseFile() calls.


The other problems most likely come from trying to read a file actively being written, until EOF.  Because the process logging to this file is still writing and _has not set EOF_ yet by closing it or calling SetEOF there is no EOF to be detected.

Perhaps it hangs waiting to read a new buffer of data, and then hangs again waiting for another after that, and so on.


Basically, what you are trying to do is impractical.  The only way I can imagine making it work at all probably requires the use of GetFileSize()/GetFileSizeEx() to figure out "where to call it quits" instead of relying on an EOF indication from the ReadFile() function.  If you really need to do this you could probably rewrite the class to work that way.

Note that you will never get all of the data though, only the part the logging process has flushed to disk.  Even then the last "line" of text you retrieve will be an incomplete line most of the time, and you may even get back "garbage" for the part of the file that hasn't been flushed-to yet.

----------


## brandoncampbell

actually something like this works fine


        Open strFile For Binary As #intFile
        strText = String$(LOF(intFile), 32)
        Get #intFile, 1, strText
        Close #intFile

        strLine() = Split(strText, vbCrLf)
        lngLineCount = UBound(strLine)

then I read the lines from the array.. later.. I read the lines starting at the last line I read based on that same array which is stored as a variable. I was just trying to see if I could get a little more speed using this method.

----------


## dilettante

Um...

strText = String$(LOF(intFile), 32)

Is the same thing I mentioned in my previous post.

But hey, since that works for you I'd say just go for it.

----------


## brandoncampbell

You're right.. so I was wondering if you think it would be better to copy the file.. for example to a .bak extension and then read the file.. then I wont risk the chance of not getting the data as it is being written.. I am not sure it would be worth changing. Have you done any speed calculations on this method as to other ways of reading files?

----------


## dilettante

The purpose of the examples I provided isn't speed, but very large file I/O.  I don't think they are partcularly faster or slower than most ways of doing file I/O though.

If you have a special case you need help with perhaps starting a question thread will get you some helpful answers.

----------


## Squall Leonhart

sorry delete this crap

----------


## Squall Leonhart

dilettante, thanks for the awsome code. 
I was about to quit my project but thanks to your code I can continue to work on it again.
Is it possible to open two files at the same time? I want to compare thier bytes one by one and decide on it, so I need both of them open at the same time, if not possible then I have to use For ... Next and bunch of huge amount of arrays which slows down the routine for sure.

----------


## dilettante

Just create an instance of the class for each file you need to have open at the same time.

----------


## green.pitch

Hello,
Thanks for sharing the Class/Module, But I am having problem on understanding its using.
would you please tell me with an example of using the module with a 2gb file?

Example: Suppose I have a 2Gb file called "hfile.dat". Now how may I read its 1GB bytes and Write them into a New file "hfile_new.dat" ?

Thanks before
Regards,

----------


## dilettante

Pretty basic.

Create two instances of the class.  Open one for read and the other for write.  Read/write in chunks of 32K or so until you have copied over everything you want.  Close both.

----------


## green.pitch

Hello,

Ok, but my question is to read bytes from the specific location and same for writing them back to another file.
In the Class, we can read file like:



```
Set hbfFile = New HugeBinaryFile
hbfFile.OpenFile "test.dat"
```

But how to read bytes from the specific location? 
Do i need to use Seek first? Which option is correct of them-



```
hbfFile.SeekAbsolute
hbfFile.SeekEnd
hbfFile.Relative
```


Thanks
Regards,

----------


## dilettante

If you want to move to the end after opening, call .SeekEnd().

If you want to move to a specific byte position, call .SeekAbsolute() passing the 0-based byte offset from the beginning.

If you want to move ahead or behind by a number of bytes, call .SeekRelative() passing a positive or negative offset from the current position.

Both .ReadBytes() and .WriteBytes() update the file position by the number of bytes read or written.

For more information look in your MSDN Library documentation for the API calls that this class uses.  All legitimate versions of VB6 come with the MSDN Library CDs.

----------


## green.pitch

Hello *dilettante*, Thanks

I'v tried but still getting errors. Here's the codes i am using -



```
Option Explicit
Private hbfFile As HugeBinaryFile

Private Sub Command1_Click()
    Dim BB() As Byte
    
    Set hbfFile = Nothing
    Set hbfFile = New HugeBinaryFile
    
    hbfFile.OpenFile "D:\xp.vmd" '3 GB size
    
    hbfFile.SeekAbsolute 524288000 '500 Mb reading
    ReDim BB(1 To 524288000) '524288000= 500 mb
    hbfFile.ReadBytes BB
    If hbfFile.IsOpen Then hbfFile.CloseFile
End Sub
```

I am getting 2 error messages:

*1.* _run-time error '7':
Out of memory_

*2.* After terminating Project and re-execute:
_Error opening file
The process cannot acces the file because it is being used by another process._

Where I'm doing wrong? Please suggest me anyone here..

Thanks

----------


## dilettante

Your first error is because there is a limit on how big the Byte array can be.

I suggest you make it no larger than 512*K*B, and then after you Seek to the starting point do a series of 512KB reads and writes until you have copied almost everything.  Finally Redim the array to fit any smaler leftover chunk at the end and read/write once more.

Something like 256KB might even be better.

The second problem occurs because the program aborts without closing the file (or files).  This leaves a file handle open and it stays open until either the compiled EXE terminates or in the IDE the IDE must terminate.  You might also use error trapping to catch any failure and then go to a "check for open, if open close the file" handler.

At this point I think you probably need to start a separate question thread in the VB6 questions forum.  You are having simple problems understanding how to write VB6 programs.

----------


## green.pitch

Hello again,
Ok that's enough for me on this topic, Problem solved after reducing the chunk size.

Thanks & Regards  :Smilie:

----------


## imarshad

Hello  dilettante,
                       I am trying to use your code in one of my projects. I have a few large files (greater than 2.5GB) and using your class I was able to scan and copy lines from the file. I just used your sample project and slightly modified it. This works absolutely fine except one problem. How can I start reading lines from a specific line position? I have 500,000 lines in my text file and I am only interested in the lines between 290,000 and 340,000. 
This code starts from line number 1 and takes almost 15 minutes to reach the lines that I require.... How can I force it to start reading lines at a specific position?


```
'Inside a loop or timer
    With htfIn
            Line = .ReadLine()
            txtLog.SelText = Line
            txtLog.SelText = vbNewLine
            currentlineNumber = currentlineNumber + 1
            labelReadProgress.Caption = "Reading: " & Format$(currentlineNumber, "#,##0")
            'htfOut.WriteLine Line
            DoEvents
    End With
```

----------


## dilettante

Well, if you knew the starting position of the "line" (remembering that a "line" is an artificial abstraction, the file is a stream of bytes) you can seek to it and read from there.

Without knowing that though (and there is no reason you would) you have little choice but to read forward counting lines as you go.

You might use the SkipLines method passing n-1 to get to line n on he next read.  That's about the only worthwhile improvement I can imagine though, and it won't be fantastically quicker.

Sorry.

----------


## imarshad

Thanks for the reply....I have done one slight modification and there is significant improvement....
            txtLog.SelText = Line
            txtLog.SelText = vbNewLine

I commented these lines (I know interaction with GUI should not have been done in the loop in the first place) but still the improvement was astonishing. Now I can loop through the complete file which btw has 470,880 lines and each line is roughly 5600 characters long in less than 1 minute. Without commenting the textbox related code it took almost 35 minutes.

----------


## dilettante

Good point.  GUI controls take a lot of processing to update and involve syncing with video hardware to avoid flicker.  Making them invisible while updating them helps, but not updating them at all does wonders.

----------


## serverguy

I wanted to thank the original author.  I have been looking for a way to rapidly handle I/O on files larger than 2GB while staying enabled and without callbacks for years.  This does it and is very fast.  Thanks a bunch for this upload!!  I wrote a more robust demo program around it and uploaded to PlanetSourceCode site for everyone to test with.  It offers variable file size creation (writing) and file copy functions with status as it goes and the ability to stop it mid-function, demonstrating that it is enabled during operation.  I tried many things and they all function as advertised.  That is rare and the quality is great.  Thanks again.

Mike




> I swear I did a search or two first but I hadn't seen this here.
> 
> HugeBinaryFile.cls is a VB6 Class that is based on an old MS KB article for VB4.
> 
> It works a bit like using Get#/Put# with Byte arrays, and supports absolute seeks to a byte position using a Currency value from 0 to far beyond the usual 2GB limit.  It can also do seeks relative to the current file position accepting a signed Long value, and seek to EOF for appending.  Its FileLen property returns the open file's length in bytes as a Currency value.
> 
> Currency was used for convenience since huge files need a 64-bit position value.  Since Currency values have an implied decimal point the class does scaling so that you can use 1 to mean 1 (1.0000) instead of using 0.0001 to mean byte 1.
> 
> If you find this imperfect you can always modify the Class to accept and return a pair of unsigned Long values instead.  In the end these can get pretty clumsy to work with though.
> ...

----------


## dilettante

Glad it is useful, but be sure to give credit to Microsoft.  If I could find the old KB article online I'd provide a link.

----------


## Black_Storm

```
Private hbfFile As HugeBinaryFile
Private hbfFilew As HugeBinaryFile
Private bytBuf() As Byte
Private bytBufencoded() As Byte

Private lngBlocks As Long
Dim MAX_BLOCKS As Long
Dim lastbytes As Long
Dim needlast As Boolean
Dim factbyte As Currency

Private Sub Command1_Click()

    On Error Resume Next

    factbyte = 1000000 ' = 1mb

    lngBlocks = 0
    lblRead.Caption = ""
    needlast = False
    Set hbfFile = New HugeBinaryFile
    Set hbfFilew = New HugeBinaryFile

    hbfFile.OpenFile "f:\test\1.mp4"
    
    Kill "f:\test\2.mp4"
    hbfFilew.OpenFile "f:\test\2.mp4"
    'hbfFilew.AutoFlush = True
    
    Caption = " Reading " _
       & Format$(hbfFile.FileLen, "##,###,###,###,##0") _
       & " bytes"

    'MAX_BLOCKS = CCur(hbfFile.FileLen \ factbyte)
    MAX_BLOCKS = CCur(Mid$(CStr(hbfFile.FileLen), 1, Len(CStr(hbfFile.FileLen)) - Len(CStr(factbyte)) + 1))
    lastbytes = CCur(hbfFile.FileLen) - CCur((MAX_BLOCKS * factbyte))
              
    Timer1.Enabled = True
End Sub


Private Sub Timer1_Timer()

    If needlast = True Then
        ReDim bytBuf(1 To lastbytes)
    Else
    
        ReDim bytBuf(1 To factbyte)
    
    End If
    
    hbfFile.ReadBytes bytBuf
    
       
    If hbfFile.EOF Then

        Timer1.Enabled = False
        hbfFile.CloseFile
        Set hbfFile = Nothing
        hbfFilew.CloseFile
        Set hbfFilew = Nothing
    Else
        
        hbfFilew.WriteBytes bytBuf
        lngBlocks = lngBlocks + 1
        
        If lngBlocks + 1 > MAX_BLOCKS Then needlast = True
        
        If lngBlocks > MAX_BLOCKS Then
            lblRead.Caption = hbfFile.FileLen
        Else
            lblRead.Caption = CCur(lngBlocks) * CCur(UBound(bytBuf))
        End If

       
       

    End If
 
End Sub
```

i edited this class and i used aes encryption too, and tested over 3 gigabyte so result is :


refrences used : 
HugeBinaryFile.cls + mdAesCtr.bas


exe project:
Attachment 184031
download exe and mp4 tested full video:
*https://up.maralhost.com/download1504.html*

----------


## Arch_Stanton

> ```
> Private hbfFile As HugeBinaryFile
> Private hbfFilew As HugeBinaryFile
> Private bytBuf() As Byte
> Private bytBufencoded() As Byte
> 
> Private lngBlocks As Long
> Dim MAX_BLOCKS As Long
> Dim lastbytes As Long
> ...


Care to share the modified class?

----------


## Arch_Stanton

Care to share the modified class?

----------


## wqweto

> Care to share the modified class?


Everything Black_Storm has cobbled together concerning encryption can be found in the original thread (including an alternative to HugeBinaryFile class itself) so post there if you need any guidance.

cheers,
</wqw>

----------

