Results 1 to 19 of 19

Thread: loading txt file to a matrix

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    7

    loading txt file to a matrix

    The dimensions of my matrix are 4, 3. I want to input a text file into a listbox. Contents of the text file are:
    4
    3
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    I need the 1 thru 12 to display in the list box like this:

    1 2 3
    4 5 6
    7 8 9
    10 11 12
    The 4 & 3 simply represent my rows and columns.
    The code has to be so I can easily change the array size.
    Here is my code so far:

    dim a(4,3) as integer
    dim row as integer
    dim col as integer

    open "a:\matrix.txt" for input as #10

    do until eof (10)


    can somebody please show me how to do this? My instructor is being a dick. Won't show us how to do this and no one can figure it out.
    PLEASE HELP???????????????

  2. #2
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    We generally don't help with homework here unless someone tries. You posted some code to show you were trying so that should protect you against a pelting from the old-timers. (We get a lot of people here who pretty much ask us to do thier homework)

    You are on the right track! You are going to use a nested loops to load your array

    VB Code:
    1. For intRow = 1 To 4
    2.             For intCol = 1 To 3
    3.                 'get values and stick them in the array here
    4.             Next intCol
    5.         Next intRow

    I made a working version, but I didn't use

    VB Code:
    1. do until eof (10)

    You already know you are reading in a set number of items.

    I've got a working sample, but you will learn nothing if we just throw code at you. See if you can get the nested loops to work and post back if you need more help.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    7

    error

    I used the nested For-next loop and I think I put the values in right, but it is giving me a "Input past end of file" error.

    Private Sub Form_Load()
    Dim intMatrix(4, 3) As Integer
    Dim introw As Integer, intcol As Integer
    Open "a:\matrix.txt" For Input As #10
    Do Until EOF(10)
    For introw = 1 To 4
    For intcol = 1 To 3
    Input #10, intMatrix(introw, intcol)
    Next intcol
    Next introw
    Loop
    Close #10
    End Sub

  4. #4
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim intMatrix(4, 3) As Integer
    3.     Dim introw As Integer, intcol As Integer
    4.     Open "a:\matrix.txt" For Input As #10
    5.         For introw = 1 To 4
    6.             For intcol = 1 To 3
    7.                 Input #10, intMatrix(introw, intcol)
    8.             Next intcol
    9.         Next introw
    10.     Close #10
    11. End Sub

    Like I mentioned, I didn't use the

    do until eof (10)

    You already know you are reading in a set number of items.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    7

    my mistake

    I forgot to take that out. However, I have taken that out and now I get no error, but nothing shows in the listbox.

  6. #6
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Here is how I loaded it in the list box . . .

    VB Code:
    1. Private Sub Command1_Click()
    2.    
    3.     Dim a(4, 3) As Integer
    4.     Dim intRow As Integer
    5.     Dim intCol As Integer
    6.     Dim strTemp As String
    7.    
    8.     Open "c:\matrix.txt" For Input As #10
    9.         For intRow = 1 To 4
    10.             For intCol = 1 To 3
    11.                 Input #10, strTemp
    12.                 a(intRow, intCol) = Val(strTemp)
    13.             Next intCol
    14.         Next intRow
    15.     Close #10
    16.    
    17.     List1.Clear
    18.     For intRow = 1 To 4
    19.         strTemp = ""
    20.         For intCol = 1 To 3
    21.             strTemp = strTemp & Space(8 - Len(Str(a(intRow, intCol)))) & a(intRow, intCol)
    22.         Next intCol
    23.         List1.AddItem strTemp
    24.     Next intRow
    25.    
    26.    
    27. End Sub

    If any of this doesn't make sense, please ask.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    7

    ????

    I see what it is the code is doing, but how did you make your text file? Mine is in the exact format below:\

    4
    3
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    all numbers in a single column.
    when I change the path you have from c: to a: which is where my txt file is stored, it is still giving the error "input past end of file" and that is using the exact code you have written.

  8. #8
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Duh, I guess I should learn to read!! I didn't even realize that the array dimensions were part of the text file. Try it like this . . .

    VB Code:
    1. Private Sub Command1_Click()
    2.    
    3.     'dim the array
    4.     Dim intMatrix() As Integer
    5.     Dim intRow As Integer
    6.     Dim intCol As Integer
    7.     Dim strTemp As String
    8.    
    9.     'Get matrix dimensions
    10.     Open "c:\matrix.txt" For Input As #10
    11.        
    12.         'get # or rows
    13.         Input #10, strTemp
    14.         intRow = Val(strTemp)
    15.         'get # of columns
    16.         Input #10, strTemp
    17.         intCol = Val(strTemp)
    18.        
    19.         'redimension the array
    20.         ReDim intMatrix(intRow, intCol)
    21.        
    22.         'load the array
    23.         For intRow = 1 To 4
    24.             For intCol = 1 To 3
    25.                 Input #10, strTemp
    26.                 intMatrix(intRow, intCol) = Val(strTemp)
    27.             Next intCol
    28.         Next intRow
    29.     Close #10
    30.    
    31.     'clear the list box
    32.     List1.Clear
    33.    
    34.     For intRow = 1 To 4
    35.         strTemp = ""
    36.         For intCol = 1 To 3
    37.             'create the list box row
    38.             strTemp = strTemp & Space(8 - Len(Str(intMatrix(intRow, intCol)))) & intMatrix(intRow, intCol)
    39.         Next intCol
    40.        
    41.         'add the list box row
    42.         List1.AddItem strTemp
    43.     Next intRow
    44.    
    45.    
    46. End Sub
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    First of all, as I understand the format of the text file, you shouldn't assume the dimensions of your array is (4, 3) since the first to numbers in the file give the dimension. So you should probably write your code so that if the file would look something like this:

    2
    3
    1
    2
    3
    4
    5
    6

    The result in the list box would be:
    1 2 3
    4 5 6

    and if the file would look like this (ALMOST the same)

    3
    2
    1
    2
    3
    4
    5
    6

    The result would be:

    1 2
    3 4
    5 6

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    OK, so I posted a little late

    BTW Armbruster, your code doesn't work since you can only redim one of the dimensions and you can't change the number of dimensions. Conclusion: You'll need to use a one-dimensional array (if you really think you need an array to start with)

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Try this code (just change the path and filename of the file you want to read)
    VB Code:
    1. Dim iRows As Integer, iCols As Integer
    2. Dim r As Integer, c As Integer
    3. Dim hFile As Integer
    4. Dim sText As String
    5. Dim sLine As String
    6.  
    7. 'Never use a constant file handle number, always use FreeFile
    8. 'to get an available file number instead
    9. hFile = FreeFile
    10. Open "c:\theFile.txt" For Input As #hFile
    11. Line Input #hFile, sText
    12. 'first line is the number of rows
    13. iRows = CInt(sText)
    14. 'read the next line
    15. Line Input #hFile, sText
    16. 'which is the number of columns
    17. iCols = CInt(sText)
    18. 'okidoki lets loop through this
    19. For r = 1 To iRows
    20.     'reset the sLine string before reading
    21.     'next set of rows
    22.     sLine = ""
    23.     For c = 1 To iCols
    24.         Line Input #hFile, sText
    25.         'add the text to the sLine string
    26.         sLine = sLine & sText & vbTab
    27.     Next
    28.     'add the line (sLine) to the listbox
    29.     List1.AddItem sLine
    30. Next
    31. 'close the file, IMPORTANT
    32. Close #hFile
    Cheers,

  12. #12

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    7

    error

    Well thank you both, but the same error still comes up using either code. "Input past end of file."

    on Armbrewsters code the error is hitting on the line in red

    Open "c:\matrix.txt" For Input As #10
    'get # or rows
    Input #10, strTemp
    intRow = Val(strTemp)
    'get # of columns
    Input #10, strTemp
    intCol = Val(strTemp)

    On Joacims code the same error is hitting on the line in red:

    'Never use a constant file handle number, always use FreeFile
    'to get an available file number instead
    hFile = FreeFile
    Open "c:\theFile.txt" For Input As #hFile

    'first line is the number of rows
    iRows = CInt(sText)
    Line Input #hFile, sText

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    That's strange. I didn't have any problem with the code. Check that you have specified the correct path to the file and that the file isn't empty.

  14. #14
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Originally posted by Joacim Andersson
    OK, so I posted a little late

    BTW Armbruster, your code doesn't work since you can only redim one of the dimensions and you can't change the number of dimensions. Conclusion: You'll need to use a one-dimensional array (if you really think you need an array to start with)
    I redimed both dimensions

    'redimension the array
    ReDim intMatrix(intRow, intCol)
    thats a valid redimension after the initial dimension of a one-dimensional array

    from MSDN . . .


    You can use the ReDim statement repeatedly to change the number of elements and dimensions in an array
    and I ran the code and it worked for me . . .
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  15. #15
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    My bad, sorry!

  16. #16
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Originally posted by Joacim Andersson
    My bad, sorry!
    It's ok, you're still a guru in my book and I haven't removed you from the christmas card list
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  17. #17

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    7

    Many Thanks!!!

    I don't know why, but it was some kinda glitch somewhere.

    Both of your codes are working. I took your advice Joacim and remade the text file. Both work fine now.


    Many thanks to both of you.

  18. #18

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    7

    Armbruster

    Could you do me a favor? Would you mind explaining to me what the following line of code is doing? I understand all the code except that.


    strTemp = strTemp & Space(8 - Len(Str(intMatrix(intRow, intCol)))) & intMatrix(intRow, intCol)

    step by step if you please.

  19. #19
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857

    Re: Armbruster

    Originally posted by garyb
    Could you do me a favor? Would you mind explaining to me what the following line of code is doing? I understand all the code except that.


    strTemp = strTemp & Space(8 - Len(Str(intMatrix(intRow, intCol)))) & intMatrix(intRow, intCol)

    step by step if you please.
    Why certainly!

    First let me explain what the space does. The space(x) function adds x number of spaces. So:

    “Carl” & space(12) & “Armbruster” would look like

    Code:
    “Carl            Armbruster”
    Next I concatenated the string. strTemp = strTemp & intMatrix(intRow, intCol) built the line of text to add to the list box.

    On the first pass, intMatrix(intRow, intCol) = 1, so strTemp = “1”
    On the next pass, intMatrix(intRow, intCol) = 2, so strTemp = “1 2”
    On the next pass, intMatrix(intRow, intCol) = 3, so strTemp = “1 2 3”

    I wanted the columns to line up and the columns to be right-justified. It is fine to left-justify text. But our “text” is numbers and numbers are usually right-justified.

    for example you wouldn’t list numbers like this:

    123
    1
    456789
    12548
    12
    5
    You would list them like this

    Code:
           123
             1
        456789
         12548
            12
             5
    I used and old-school method of right-justifying columns. I just padded the columns with spaces. I call this an old school method, because when you did this with old dos applications, the fonts were fixed-length (every letter takes up the same amount of space - like courier-new) so when you padded with spaces, they would all line up perfectly. So to make the columns line up I added 8 spaces – then length (that is where the len() function comes in) of the text that I was lining up. Len is a function that measure the length of a string so I made sure that it was a string by using the str() function to convert the integer to a string.

    Here is another example of what I did:

    VB Code:
    1. Private Sub Command1_Click()
    2.     List1.Font = "Courier New"
    3.     List1.AddItem "Not justified"
    4.     List1.AddItem ""
    5.     List1.AddItem "Carl" & Space(15) & "Armbruster"
    6.     List1.AddItem "Fred" & Space(15) & "Flinstone"
    7.     List1.AddItem "Billy" & Space(15) & "Gates"
    8.     List1.AddItem "Santa" & Space(15) & "Clause"
    9.    
    10.     List1.AddItem ""
    11.     List1.AddItem ""
    12.     List1.AddItem "Right justified"
    13.     List1.AddItem ""
    14.     List1.AddItem "Carl" & Space(25 - Len("Carl") - Len("Armbruster")) & "Armbruster"
    15.     List1.AddItem "Fred" & Space(25 - Len("Fred") - Len("Flinstone")) & "Flinstone"
    16.     List1.AddItem "Billy" & Space(25 - Len("Billy") - Len("Gates")) & "Gates"
    17.     List1.AddItem "Santa" & Space(25 - Len("Santa") - Len("Clause")) & "Clause"
    18.  
    19.  
    20. End Sub

    and it looks like this . . .



    Is it starting to make sense?

    Hope this helps and good luck with the projects . . .
    Attached Images Attached Images  
    Last edited by Armbruster; May 14th, 2003 at 08:26 PM.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

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