×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Reading and Parsing a text file into VB

Reading and Parsing a text file into VB

Reading and Parsing a text file into VB

(OP)
I used to work with Excel Vb files where I would open the text file in Excel and manipulate the information from Visual Basics.  The files have gotten much bigger and Excel can only handle 65000 lines.  I need to open the file directly into VB and assign each parsed line to a multi dimensional array.  

Second question - if you have an multidimentional array - how do I sort on that?

I would appreciate some code samples. thanks

RE: Reading and Parsing a text file into VB

How is your text file formatted? Is each line comma delimeted? How many dimensions is your array going to contain? What type of data are you reading in and how do you want to sort the values?

Unless you know how many rows of data you have, it would be better to dynamically change the size of the array. Unfortunatly, you can not do that on a multi-dimensional array. Here is a sample, where the text file contains this:

text.txt
1,2,3
4,5,6
7,8,9


Option Explicit
Option Base 1

Sub ImportData()
    Dim sFile As String, sMsg As String
    Dim Val1() As Integer, Val2() As Integer, Val3() As Integer
    Dim iIdx As Long
    
    sFile = "C:\test.txt"
    
    iIdx = 1
    Open sFile For Input As #1
        Do While Not EOF(1)
            ReDim Preserve Val1(iIdx)
            ReDim Preserve Val2(iIdx)
            ReDim Preserve Val3(iIdx)
            Input #1, Val1(iIdx), Val2(iIdx), Val3(iIdx)
            iIdx = iIdx + 1
        Loop
    Close
    
    sMsg = "Values:" & vbCrLf
    For iIdx = LBound(Val1) To UBound(Val1)
        sMsg = sMsg & "Idx: " & iIdx & " - (" & _
               Val1(iIdx) & ", " & Val2(iIdx) & _
               ", " & Val3(iIdx) & ")" & vbCrLf
    Next iIdx
    
    MsgBox sMsg
    
    ReDim Val1(1): ReDim Val1(2): ReDim Val1(3)

End Sub

NOTE: If you are going to read in your large file, take the MsgBox portion out. I don't think it will like you too much if you try to stuff that huge string in there.

Hope this helps to get you started. I have some sorting routines, but I need a little more info first.

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.

RE: Reading and Parsing a text file into VB

Try this,

dim ff as integer
dim raw as string
dim Fname as string 'the path to the file
dim lines() as string 'used to hold the lines of the text file

ff = FreeFile

Open Fname For Binary As #ff
Raw = String$(LOF(ff), 32)
Get #ff, 1, Raw
Close #ff
lines() = Split(Raw, vbnewline) 'this assumes that the data is stored in individual lines.

dim multiArray(1 to upper(lines),1 to 5, 1 to 7) as variant 'or whatever datatype

Instead of a multi dimensional array I would suggest using a collection of objects.

To sort the array, use one of the classical sorting algorithms, bubble, insertion, quick sort....

If you would give more details about how the data is formated in the text file I could be of more assistance.


Troy Williams
fenris@hotmail.com

RE: Reading and Parsing a text file into VB

i want to make a vb program tts usin a wav file and dsound files

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close