reading a text file with spaces

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dawn123
    New Member
    • Nov 2009
    • 15

    reading a text file with spaces

    I have a text file containing many lines of numbers seperated by spaces. example:
    1234 34332 12
    1235 3543 12
    I need to read the file into a 2-d array I dont know how to do this becasue of the spaces. I need to replace the spaces in that file with commas, so i can do this. or anyother way that is possible..
  • vb5prgrmr
    Recognized Expert Contributor
    • Oct 2009
    • 305

    #2
    Use the replace function when you read it in. If you do not know how to read a file in with VB 6.0, the please look up the following on the index tab of vb's help...

    FreeFile Function
    Open Statement
    Input Function
    Line Input Function
    Close Statement
    Replace Function
    Split Function



    Good Luck

    Comment

    • !NoItAll
      Contributor
      • May 2006
      • 297

      #3
      Also look up the split function. Very handy for just what you are doing.

      Comment

      • vb5prgrmr
        Recognized Expert Contributor
        • Oct 2009
        • 305

        #4
        Ah-hmm (clears throat and then whispers) Psstt! Hey NoItAll, look at the line right above my Good Luck :)

        Comment

        • !NoItAll
          Contributor
          • May 2006
          • 297

          #5
          DOH!

          ...he said with an embarrassed red-faced......

          Don't forget the ! in front of NoItAll, it is meant, and proven here, as the "not" symbol in my pseudonym...

          Comment

          • vb5prgrmr
            Recognized Expert Contributor
            • Oct 2009
            • 305

            #6
            dawn123,

            Recieved your PM...

            Code:
            Option Explicit
            
            Private Sub Command1_Click()
            
            On Error GoTo Command1_ClickError
            
            Dim FName As String, FNumb As Integer
            Dim LineContents As String, LineElements() As String
            Dim LowerBoundsOfArray As Integer, UpperBoundsOfArray As Integer, ForLoopCounter As Integer
            
            CD.CancelError = True
            CD.Filter = "Text Files *.txt|*.txt"
            CD.ShowOpen
            
            FNumb = FreeFile
            FName = CD.FileName
            
            Open FName For Input As #FNumb
            Do While Not EOF(FNumb)
              Line Input #FNumb, LineContents
              LineElements = Split(LineContents, " ")
              'do work here with information from file LineElements(0), LineElements(1), LineElements(2)
              'you may also want to use the LBound and UBound Functions...
            '  LowerBoundsOfArray = LBound(LineElements)
            '  UpperBoundsOfArray = UBound(LineElements)
            '  For ForLoopCounter = LowerBoundsOfArray To UpperBoundsOfArray
            '    'do work here
            '  Next ForLoopCounter
            Loop
            Close #FNumb
            
            Exit Sub
            Command1_ClickError:
            
            If Err.Number = 32755 Then Exit Sub 'user pressed cancel
            
            End Sub
            Just for starters... Now, as for replacing those space characters with commas, you can either use the replace function as I mentioned and remove the Split Function from my example code, or you can use the Join Function and keep my example split function, however I must note that this would be much slower than use the replace function.



            Good Luck

            Comment

            Working...