Click to See Complete Forum and Search --> : tab delimited text file
pgrimes
Jun 21st, 2002, 11:40 AM
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:
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
pgrimes
Jun 24th, 2002, 10:20 AM
please, can anyone help me with this?
Musician
Jun 24th, 2002, 11:20 AM
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
pgrimes
Jun 24th, 2002, 11:49 AM
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:
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?
Musician
Jun 24th, 2002, 01:33 PM
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.
pgrimes
Jun 24th, 2002, 02:30 PM
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
DevGrp
Jun 24th, 2002, 03:03 PM
Would it be possible to use and XML file? I think that would be a better choice than and txt file.
pgrimes
Jun 24th, 2002, 03:08 PM
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 :(.
pgrimes
Jun 24th, 2002, 03:21 PM
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
Musician
Jun 24th, 2002, 03:24 PM
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?
Musician
Jun 24th, 2002, 03:26 PM
Whoops misread that last post of yours. I'll do this code complete and post it shortly.
pgrimes
Jun 24th, 2002, 04:10 PM
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
Musician
Jun 25th, 2002, 03:01 AM
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.
pgrimes
Jun 25th, 2002, 10:25 AM
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:
<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:
<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:
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?
Musician
Jun 25th, 2002, 01:04 PM
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.
pgrimes
Jun 25th, 2002, 01:49 PM
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
pgrimes
Jun 25th, 2002, 01:54 PM
actually, if anybody's interested, it needed an IsNumeric() function so it didn't write a "<td>" for every space.
arBlocks = arLines(x).Split(" ")
For y = 0 To UBound(arBlocks)
'now we do each column - <td>
If IsNumeric(arBlocks(y)) Then
Response.Write("<td>" & arBlocks(y) & "</td>")
End If
Next y
Thanks again
pgrimes
Jul 1st, 2002, 12:01 PM
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:
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 :(
Musician
Jul 1st, 2002, 12:54 PM
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?
pgrimes
Jul 1st, 2002, 01:16 PM
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?
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.