Sub DUE()

Dim strBuffer As String
Dim strContents() As String
Dim I As Long

ReDim strContents(0)
Erase strContents()

Close #1
Open "C:\TEST\28-06-2013.TXT" For Input As #1
strBuffer = Input(LOF(1), 1)
Close #1

strContents = Split(strBuffer, vbCrLf)

For I = LBound(strContents) To UBound(strContents)
    'Debug.Print strContents(I)
Next I

End Sub

Problem out of Memory!!!!

When buffering the txt file. Is approx 256 mb 1.500.xxx lines. Each line have a fixed lenght 132.

You might want to read this. Just playing with it the Erase sets UBound to -1. Skip the erase

You're probably better off using FileSystemObjects to read your file. The problem with the approach you're using is that the program tries to reserve string space equal to the size of the entire file. If there are other programs or services running on your machine there might not be enough space to accommodate the request. And, if you run your program once, it might run fine, and fail the next time.

Here's a snippet of code to get you started:

' be sure to include the reference to Microsoft Scripting Runtime in Project/References
Dim myFSO As Scripting.FileSystemObject
Dim myTextStream As Scripting.TextStream
Dim i As Long
Dim myString As String
Dim sFilePath As String
    'Create a new FileSystemObject
sFilePath = "e:\TEST\myCopy.TXT"
Set myFSO = New Scripting.FileSystemObject
    'Make sure file exists:
If myFSO.FileExists(sFilePath) Then
    Set myTextStream = myFSO.OpenTextFile(sFilePath, ForReading)
    i = 1
    Do Until myTextStream.AtEndOfStream
        myString = myTextStream.ReadLine
        Debug.Print Str(i) + ": " + myString
        i = i + 1
    Loop
    myTextStream.Close
End If
Set myTextStream = Nothing
Set myFSO = Nothing

I tested this successfully with a 310Mb file.

Hope this helps! Happy coding!

If your goal is to read the file line by line without the carriage return and line feed (CRLF), you can do so without allocating large amount of memory. And, I still opt the method you choose than using another object for that purpose.

Dim strBuffer As String

Close #1 ' To make sure that 1 is available

Open "C:\TEST\28-06-2013.TXT" For Input As #1

While Not Eof(1)
    Line Input #1, strBuffer
    Debug.Print strBuffer
Wend

Close #1

@RonalBertogi Yes, your method will work fine.

I generally use the FileSystemObject because of the other methods available...specifically in this case, the ability to easily check for the existence of a file. In your technique, if you try to open a file that doesn't exist, it will throw a run-time error 76 (Path not found) that either has to be handled in an "OnError goto" or otherwise interrupt processing.

You are correct, @BitBlt. I just answered the problem posted.

About checking if a particular file exists, the usual trick is to attempt to open a file for sequential input (in C/C++/VB) and in C: check for the file number returned if it indicates a valid file number or an error code; in C++/Win32 File IO: check if the returned handle is valid; and, in VB, use its structured error handling mechanism (On Error goto...) that uses the IErrorInfo interface. This method will not just allow you to check for a file's existence but also IO privileges or file read/write access.

BTW, the fastest way to check if a file exists is to use the master file allocation table and that's another story.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.