Results 1 to 15 of 15

Thread: open large text files

  1. #1

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755

    open large text files

    i use this code to open text files, but it cant open large ones...
    is there a better code?

    VB Code:
    1. Open Filename For Input As #1
    2.        Do While Not EOF(1)
    3.           Text1 = Input(LOF(1), 1)
    4.        Loop
    5.     Close #1
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  2. #2
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    VB Code:
    1. Open Filename For Input As #1
    2.       Text1.Text = Input(LOF(1), #1)
    3. Close #1
    Note: try not to rely on DEFAULT property but always provide them explicitely.
    McGenius

  3. #3
    Junior Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    23
    Hmmm... this wouldn't be related to your "slow variables" post by any chance?

    Would I be right in guessing that you're using a textbox control to show the contents of a text file? That you read in the contents of the file and append them one character at a time to the text field in the control?

    You really want to be using the Microsoft Scripting Runtime component for this. It is a better choice for getting your data out of the file. Create a FileSystem object and from this, get a TextStream and then use the ReadAll method.

    Does this help?

    Dave

  4. #4
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Input$ function works just as fast BUT it doesn't depend on any library what-so-ever (except for VB runtimers). Also, KIM that Scripting library was NOT developed for use in VB but ASP primarely.
    McGenius

  5. #5
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    Hmm, isn't a VB textbox limited to 65535 chars? I'm not sure...

  6. #6
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Originally posted by riis
    Hmm, isn't a VB textbox limited to 65535 chars? I'm not sure...
    Not exactly. Here is a quote from MSDN:
    ... The Text setting for a TextBox control is limited to 2048 characters unless the MultiLine property is True, in which case the limit is about 32K
    McGenius

  7. #7
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    Then that's the reason that Cyborg can't open large text files (larger than 32k). He should store the contents in a buffer if he needs to have the entire text file accessible by the application at once.

  8. #8
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    1) Don't bother with the scripting object (too much resource comitted for such a simple operation, plus you may have distribution problems)

    2) Don't use a For-Next loop to read in the text.... that will be take much longer for larger files. Instead, use one Get statement, which invokes OS API's...


    You can simply
    1) declare a string
    2) buffer the string to size of the file
    3) load the file contents directly into the string

    VB Code:
    1. Dim myFileData As String
    2. myFileData = Space(FileLen("C:\myfile.txt"))
    3. Open "C:\myfile.txt" For Binary As #1
    4. Get #1,,myFileData
    5. Close #1

    If you need to display only a portion of the filedata, use Mid$()...
    Last edited by nemaroller; Apr 2nd, 2003 at 11:01 AM.

  9. #9
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by nemaroller
    VB Code:
    1. Dim myFileData As String
    2. ReDim myFileData(FileLen("C:\myfile.txt"))
    3. Open "C:\myfile.txt" For Binary As #1
    4. Get #1,,myFileData
    5. Close #1
    Suerly you mean:

    VB Code:
    1. Dim myFileData As String
    2. [b]myFileData = Space(Lof(1))[/b]
    3. Open "C:\myfile.txt" For Binary As #1
    4.     Get #1, , myFileData
    5. Close #1
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  10. #10
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Surely I did!

    Sorry bout that.... I usually work with Byte arrays instead... so it was a matter of habit... i'll change it...

  11. #11
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    Re: open large text files

    Originally posted by cyborg
    i use this code to open text files, but it cant open large ones...
    is there a better code?

    VB Code:
    1. Open Filename For Input As #1
    2.        Do While Not EOF(1)
    3.           Text1 = Input(LOF(1), 1)
    4.        Loop
    5.     Close #1
    Use a RichTextBox instead of a standard one. Youll have to add it from the components menu first. Your problem is likely that a standard textbox cannot hold the amount of data (as someone already mentioned).

  12. #12
    Frenzied Member McGenius's Avatar
    Join Date
    Jan 2003
    Posts
    1,199
    Originally posted by riis
    Then that's the reason that Cyborg can't open large text files (larger than 32k). He should store the contents in a buffer if he needs to have the entire text file accessible by the application at once.
    Hey, RTB can come handy ..., but you are the one to decide.
    McGenius

  13. #13

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    thanks alot guys! you've been really helpful!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  14. #14
    Member awuh0's Avatar
    Join Date
    Feb 2003
    Posts
    53

    lol...

    is there a better way to go threw a plain text file thats 6.7MB???
    and changeing just about every line...

  15. #15
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    nice signature btw...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width