ZeroEffect Posted December 18, 2006 Posted December 18, 2006 Ok I have a program that imports ASCII text file to create a DBF file. Just creating the DBF file is not an option, if it was life would be grand. The ASCII text file has a format that goes as follows The complete line is 132 characters long and based on the information in the line more or less information is added to the new file. the 132 characters are just 20 columns with no delimiters. i have created an excell file that will create the above structure but I can only export it with delimiters. Sounds easy enough, read the exported text file into an array the write the array back to a new text file, but I am having some issues. If there is nothing in a column I have to create the spaces to show no data has been entered. If I step through the data everything looks good but when it writes to a file randomly some lines are off by on character. Here is some code. 'Ok all the backslashes have been removed when I post and I 'can't remember how to make the show up. Private Function GetLog() Dim lines As String Dim info() As String Dim myLog As String = "C:\Dad\Import\FreshExport.txt" Dim srr As StreamReader Dim i As Integer Dim j As Integer Dim strformatedline As String Dim itm As ListViewItem Try If File.Exists(myLog) Then srr = New StreamReader(myLog) lines = srr.ReadToEnd info = lines.Split(Environment.NewLine) For i = LBound(info) To UBound(info) Dim tempArray() As String tempArray = info(i).Split(vbTab) strformatedline = "" itm = ListView1.Items.Add(tempArray(0)) For j = 1 To UBound(tempArray) itm.SubItems.Add(tempArray(j)) Next For j = LBound(tempArray) To UBound(tempArray) strformatedline = strformatedline & BuildString(tempArray(j), (j + 1)) Next If strformatedline.Length < 132 Then strformatedline = BuildString(strformatedline, 0) End If BuildFile(strformatedline) Next Else MessageBox.Show("File not found") End If Catch ex As Exception MessageBox.Show("Error reading file: " & ex.Message) Finally If Not srr Is Nothing Then srr.Close() End If End Try End Function Public Function BuildString(ByVal strData As String, ByVal intCol As Integer) Try Dim strTempData As String If intCol = 0 Then 'This is just used incase there are blank lines strTempData = BuildSpacing(strData, 132) ElseIf intCol = 1 Then strTempData = BuildSpacing(strData, 5) ElseIf intCol = 2 Then strTempData = BuildSpacing(strData, 1) ElseIf intCol = 3 Then strTempData = BuildSpacing(strData, 8) ElseIf intCol = 4 Then strTempData = BuildSpacing(strData, 2) ElseIf intCol = 5 Then strTempData = BuildSpacing(strData, 1) ElseIf intCol = 6 Then strTempData = BuildSpacing(strData, 1) ElseIf intCol = 7 Then strTempData = BuildSpacing(strData, 1) ElseIf intCol = 8 Then strTempData = BuildSpacing(strData, 8) ElseIf intCol = 9 Then strTempData = BuildSpacing(strData, 1) ElseIf intCol = 10 Then strTempData = BuildSpacing(strData, 8) ElseIf intCol = 11 Then strTempData = BuildSpacing(strData, 8) ElseIf intCol = 12 Then strTempData = BuildSpacing(strData, 1) ElseIf intCol = 13 Then strTempData = BuildSpacing(strData, 35) ElseIf intCol = 14 Then strTempData = BuildSpacing(strData, 10) ElseIf intCol = 15 Then strTempData = BuildSpacing(strData, 7) ElseIf intCol = 16 Then strTempData = BuildSpacing(strData, 7) ElseIf intCol = 17 Then strTempData = BuildSpacing(strData, 7) ElseIf intCol = 18 Then strTempData = BuildSpacing(strData, 7) ElseIf intCol = 19 Then strTempData = BuildSpacing(strData, 7) ElseIf intCol = 20 Then strTempData = BuildSpacing(strData, 7) End If 'MsgBox(intCol & " " & strTempData.Length) Return strTempData Catch ex As Exception End Try End Function Private Function BuildSpacing(ByVal strData As String, ByVal intTLength As Integer) As String Dim modata As String Dim i As Integer modata = strData For i = strData.Length + 1 To intTLength modata = modata & " " Next Return modata End Function Public Sub BuildFile(ByVal strData As String) Try If System.IO.File.Exists("C:\Dad\import\tempFile.log") = False Then Dim SW As New IO.StreamWriter("C:\Dad\import\tempFile.log", False, System.Text.ASCIIEncoding.ASCII) SW.Write(strData & vbCr) SW.Close() Else Dim SW As New IO.StreamWriter("C:\Dad\import\tempFile.log", True, System.Text.ASCIIEncoding.ASCII) SW.Write(strData & vbCr) SW.Close() End If Catch ex As Exception End Try End Sub Any thoughts? you will find in the buildstring function the size of each column. I am lost as to why this is happening. I have attached an example file created by this application Thanks for any thought you may have. ZeroEffecttempFile.txt Quote If you can't find it, Build It. There is no place Like 127.0.0.1 also don't forget 1 + 1 = 10
Eduardo Lorenzo Posted December 18, 2006 Posted December 18, 2006 try to TRIM the string before sending it to BuildSpacing or inside buildspacing. Private Function BuildSpacing(ByVal strData As String, ByVal intTLength As Integer) As String Dim modata As String Dim i As Integer modata = strData.Trim <--- here For i = strData.Length + 1 To intTLength modata = modata & " " Next Return modata End Function Quote
ZeroEffect Posted December 19, 2006 Author Posted December 19, 2006 Well I gave "Trim" a try and well that helped in some ways but hosed me in others. So that is not going to work. Here is what was going on with trim. If my string was not 132 in length then it would be built just adding spaces to the end to make it 132, trim removed all the spaces and well that isn't good. Hmm I'll keep playing around any other ideas? Thanks ZeroEffect Quote If you can't find it, Build It. There is no place Like 127.0.0.1 also don't forget 1 + 1 = 10
MrPaul Posted December 19, 2006 Posted December 19, 2006 TrimEnd Trim removes both leading and trailing characters from a string. TrimStart removes only leading characters and TrimEnd removes only trailing characters. Both of these methods take a parameter specifying which characters are to be removed: Private Function BuildSpacing(ByVal strData As String, ByVal intTLength As Integer) As String Dim modata As String Dim i As Integer modata = strData.TrimEnd(" "c) 'Remove trailing spaces For i = modata.Length + 1 To intTLength modata = modata & " " Next Return modata End Function As a side note, you could make BuildSpacing more efficient by using the StringBuilder class. Good luck :cool: Quote Never trouble another for what you can do for yourself.
ZeroEffect Posted December 19, 2006 Author Posted December 19, 2006 Thanks I'll look into the StringBuilder class. Quote If you can't find it, Build It. There is no place Like 127.0.0.1 also don't forget 1 + 1 = 10
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.