Results 1 to 20 of 20

Thread: tab delimited text file

  1. #1

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342

    tab delimited text file

    I'm using vb .net (asp .net) to read the data in a text file. I have no problem listing all of the data, but I need to separate it by the columns it's in. The data is tab delimited in the text file. I know I need to use the split function, but I'm not really used to the StreamRead thing and can't figure it out.

    Here is an example of the text file:
    1.15 106.0 3.341 0.06
    1.20 103.2 2.889 0.05
    1.25 76.0 3.592 0.05
    1.30 82.7 3.331 0.05
    1.35 138.1 3.219 0.05
    1.40 84.3 2.656 0.05
    .
    .
    .

    It goes on like that for a while. I've tried to load each column int a datagrid also, I couldn't get that to work either.

    Here is some of the code I came up with:
    Code:
            Dim strArray() As String
            Dim columnDelimiter As Char
            Dim strField As String
    
            columnDelimiter = "\t"
    
            oStreamRead = File.OpenText("C:\cpt\CPT-04.txt")
    Do
            oStreamLine = oStreamRead.ReadLine
            strArray = Split(oStreamLine, columnDelimiter)
            Response.Write(strArray)
    
    Loop Until oStream.ReadLine = ""
    Thanks for any help

    Paul

  2. #2

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    please, can anyone help me with this?

  3. #3
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    The best solution is to have a Line array and a blocks array, say arLines, arBlocks then put the whole text file into one string so you can close the stream:-

    Dim strContent As String = oStreamRead.ReadToEnd
    oStreamRead.Close()
    'Now split the lines on a LineFeed
    arLines = strContent.Split(vbLf)

    Dim x, y As Integer
    For x = 0 to UBound(arLines)
    arBlocks = arLines(x).Split(vbTab)
    For y = 0 to UBound(arBlocks)
    'deal with each column as you see fit - into listview
    'for example where arBlocks(y) is content of column
    Next y
    Next x
    Last edited by Musician; Jun 24th, 2002 at 11:24 AM.

  4. #4

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    Thanks a lot for the reply musician, I'm a little confused though.

    How for example would I display all of the values in the second column?

    I tried this:
    Code:
    Dim strContent As String = oStreamRead.ReadToEnd 
    oStreamRead.Close() 
    'Now split the lines on a LineFeed 
    arLines = strContent.Split(vbLf) 
    
    Dim x, y As Integer 
    For x = 0 to UBound(arLines) 
    arBlocks = arLines(x).Split(vbTab) 
    For y = 0 to UBound(arBlocks) 
     Response.Write(arBlocks(y))
    'deal with each column as you see fit - into listview 
    'for example where arBlocks(y) is content of column 
    Next y 
    Next x
    When I do that it writes out the whole file, it does the same thing when I "Resposne.Write(arLines(x))"

    How would I just show the first or second, or the xth column?

  5. #5
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    O.k. I'll assume you are talking about asp.net here so I'll give you code to put it into a table:-

    Dim strContent As String = oStreamRead.ReadToEnd
    oStreamRead.Close()
    'Now split the lines on a LineFeed
    arLines = strContent.Split(vbLf)

    Dim x, y As Integer
    'the table would have to be created before the loops
    Response.Write("<table border=1>")
    For x = 0 to UBound(arLines)
    'for each line we would need a row
    Response.Write("<tr>")
    arBlocks = arLines(x).Split(vbTab)
    For y = 0 to UBound(arBlocks)
    'now we do each column - <td>
    Response.Write("<td>" & arBlocks(y) & "</td>")
    Next y
    'close the row
    Response.Write("</tr>")
    Next x
    'close the table
    Response.Write("</table>")

    When you split a line into blocks the first column will the 0 element up to the last column being the upper bound. Splitting creates an array in the order it finds it, i.e. from your example line1 would be:-


    arBlocks(0) = 1.15
    arBlocks(1) = 106.0
    arBlocks(2) = 3.341
    arBlocks(3) = 0.06

    The code relies on every line having the same amount of columns and once you have the columns split into an array you can use the ubound function to go from the 0 element to the top element.
    The DataGrid is designed for DataTables and you would have to create a DataSet and DataTable to hold your columns before binding them to the DataGrid. Thats why I used a table as a more straightforward example.

    In your example you used Response.Write(arBlocks(y)) so this would just print out each element one after the other. You have to code some kind of way of seperating the elements to avoid this.

  6. #6

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    Thanks again for the reply... appreciated .

    I just got this error when I ran the code:
    Value cannot be null. Parameter name: Argument 'Array' is Nothing.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: Argument 'Array' is Nothing.

    Source Error:


    Line 65: 'the table would have to be created before the loops
    Line 66: Response.Write("<table border=1>")
    Line 67: For x = 0 To UBound(arLines)
    Line 68: 'for each line we would need a row
    Line 69: Response.Write("<tr>")


    Source File: G:\WebServer\SVGgraph\WebForm1.aspx.vb Line: 67
    This is the format of the file:
    0.65 375.6 12.506 0.05
    0.70 263.6 10.692 0.05
    0.75 161.6 8.031 0.06
    There are about 200 + lines, and every one has the same amount of columns.

    If you have any ideas about the error message, please let me know.

    Thanks

    Paul

  7. #7
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Would it be possible to use and XML file? I think that would be a better choice than and txt file.

  8. #8

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    Ya, I would much rather use an xml file, or something out of a database, but... These files are created by some data acquisition hardware, and they only create thes ".CPD" files which, are tab delimited text files .

  9. #9

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    Sorry, the error was my mistake, I got it to display, but, it displayed the same thing as before. I still have the same problem...

    Instead of the array values containing this:
    arBlocks(0) = 1.15
    arBlocks(1) = 106.0
    arBlocks(2) = 3.341
    arBlocks(3) = 0.06
    (which is what I want),

    They contain this:

    arBlocks(0) = 1.15 106.0 3.341 0.06
    arBlocks(1) = 1.20 103.2 2.889 0.05
    arBlocks(2) = 1.25 76.0 3.592 0.05
    arBlocks(3) = 1.30 82.7 3.331 0.05

    Each array 'cell' contains a whole row instead of one column value.


    Thanks,

    Paul

  10. #10
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    The error you are getting is because arLines is an empty array. Is it declared properly? Have you left out the arLines = strContent.Split(vbLf) function?

  11. #11
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    Whoops misread that last post of yours. I'll do this code complete and post it shortly.
    Last edited by Musician; Jun 24th, 2002 at 03:30 PM.

  12. #12

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    The error you are getting is because arLines is an empty array. Is it declared properly? Have you left out the arLines = strContent.Split(vbLf) function?
    Ya, I did leave out that function.

    Just a heads up, I need to split @ vbTab, not vbLF. I did change that in my code.

    thx

  13. #13
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    Yes well the code I gave you splits the text into lines first on vbLF and then into blocks on vbTab. I think you should check the code that I gave you and make sure you have it exactly as I had it.

  14. #14

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    Thanks again for all the replies,

    I understand what the code should be doing, but it isn't, posted below is the code that I have. It should create a "<tr>" for each line that is read, and a "<td>" for each column within each line, but it isn't. Here is what the html it creates looks like:
    Code:
    <table border=1>
     <tr>
         <td>  0.65    375.6  12.506    0.05</td>
     </tr>
     <tr>
         <td>  0.70    263.6  10.692    0.05</td>
     </tr>
     <tr>
         <td>  0.75    161.6   8.031    0.06</td>
     </tr>
    </table>
    So, it appears that arBlocks(x) a whole line rather than once column. The html should look like this:
    Code:
    <table border=1>
     <tr>
         <td>  0.65</td>
         <td> 375.6</td>
         <td>12.506</td>
         <td>0.05</td>
     </tr>
    </table>


    Again, here is the code I'm using:
    Code:
            Dim oStreamRead As StreamReader
    
            oStreamRead = File.OpenText("G:\WebServer\test\SVG\cptFiles\CPT-01a.CPD")
    
            Dim arLines() As String
            Dim arBlocks() As String
    
            Dim strContent As String = oStreamRead.ReadToEnd
            oStreamRead.Close()
            'Now split the lines on a LineFeed 
            arLines = strContent.Split(vbLf)
    
            Dim x, y As Integer
            'the table would have to be created before the loops 
            Response.Write("<table border=1>")
            For x = 0 To UBound(arLines)
                'for each line we would need a row 
                Response.Write("<tr>")
                arBlocks = arLines(x).Split(vbTab)
                For y = 0 To UBound(arBlocks)
                    'now we do each column - <td> 
                    Response.Write("<td>" & arBlocks(y) & "</td>")
                Next y
                'close the row 
                Response.Write("</tr>")
            Next x
            'close the table 
            Response.Write("</table>")
    Am I badly overlooking an error in the code? Is there a reference I need to make for the split function to work correctly?

  15. #15
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    I don't think .Net is seeing any tabs so I would try splitting on the exact amount of spaces between each element:-

    arBlocks = arLines(x).Split(" ")

    To be certain copy and paste the gap in the text file between 2 numbers into that statement between the inverted-commas. The split is only resulting in one element i.e. arBlocks(0) and no more because it didn't find any tabs in the line to split on.

  16. #16

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    aha... good thinking, that was the problem...

    I changed it to split at " " and it works .

    Sorry I didn't think of that earlier. Thanks a lot for the help





    Paul

  17. #17

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    actually, if anybody's interested, it needed an IsNumeric() function so it didn't write a "<td>" for every space.

    VB Code:
    1. arBlocks = arLines(x).Split(" ")
    2.             For y = 0 To UBound(arBlocks)
    3.                 'now we do each column - <td>
    4.                 If IsNumeric(arBlocks(y)) Then
    5.                     Response.Write("<td>" & arBlocks(y) & "</td>")
    6.                 End If
    7.             Next y


    Thanks again

  18. #18

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    Hi again, I'm running into a problem once again . Some of the files I'm using don't have the same number of spaces between each column.

    I don't think .Net is seeing any tabs so I would try splitting on the exact amount of spaces between each element:-

    arBlocks = arLines(x).Split(" ")

    To be certain copy and paste the gap in the text file between 2 numbers into that statement between the inverted-commas. The split is only resulting in one element i.e. arBlocks(0) and no more because it didn't find any tabs in the line to split on.


    Once again, the files look like this:
    Code:
      0.65  375.6  12.506 0.05 
      0.70  263.6  10.692 0.06 
      0.70   63.1   8.713 0.06
      0.70   92.6   3.462 0.06
    The colums are aligned at the right edge, so depending on the number of digits a number has, it will be a different amount of spaces from the other column.

    man... I wish vb would just see that as a tab

  19. #19
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    Looking at that output no code is going to see that as a tab - the column on the far right appears to only have one space as a seperator.
    What program is making these files? Can you specify a different seperator like a comma or something?

  20. #20

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    Looking at that output no code is going to see that as a tab - the column on the far right appears to only have one space as a seperator.
    I'm having trouble preserving the exact format of the file in my posts (I'll attach one of the actual files in this post).

    The column on the far right is actually separated from the one next to it by 4 spaces.

    The second column is seperated from the first by 4 spaces if the number in the second column is in the hundreds, 5 spaces if the number is in the 10's etc.

    Unfortunately, I have no control over how these files are created by the device... A lot of them are archived, and are several years old. Any new files created will be in the exact same format.

    Thanks for the reply once again!

    Paul

    ps.
    Am I the only one experiencing extremely slow page loads on vbforums lately?

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