|
Question : writing a fixed length text file
|
|
I currently have a routine which creates a delimited text file but I have to have a fixed length text file.
How can I change the following code to accomplish this?
(vb.net with a sqlce database)
Dim sw As StreamWriter = New StreamWriter("Transactions.txt") Try cn.Open() cnCmd.CommandText = "Select description, document, part_num, cur_qty, cond_code, generic1, generic2 from tblinventory" cnCmd.Connection = cn drDB = cnCmd.ExecuteReader
While drDB.Read sw.WriteLine(drDB.Item("description") & drDB.Item("document") & "LINE" & "," & drDB.Item("part_num") & "," & "RIC" & "," & "UI" & "," & drDB.Item("cur_qty") & "," & drDB.Item("cond_code") & "," & drDB.Item("document") & "," & drDB.Item("generic1") & "," & drDB.Item("generic2")) End While drDB.Close()
cn.Close()
Thanks for any assistance
|
|
Answer : writing a fixed length text file
|
|
I think both posts above are a little simplistic in their approach. Most fixed width text files do not simply have a fixed width for the entire line...instead, each column of data usually has a specific width so you need to pad each individual piece of data coming out of your database.
In your example you have these pieces of data:
description, document, part_num, cur_qty, cond_code, generic1, generic2
I'm guessing that description, document cond_code, generic1 and generic2 are just normal alphanumeric strings that need to be left aligned and padded on the right with spaces. But part_num and cur_qty look like they might contain actual numbers. Often in fixed width files we right align numbers and pad the left with zeroes.
So in the code below, I first extract all the values from the database into individual variables. Then I build the "line" variable by adding all the pieces with the appropriate padding. You will need to adjust these numbers to fit your requirements. I made description have a width of 30, while the document field had a width of 25, etc...
Notice that in the part_num and cur_qty fields, the format is different. I used PadLeft() instead of PadRight() and specified a "0" as the fill character. So if the cur_qty was 123, it would appear as "00123" because I specifed a length of 5 with "0" as the padding character.
Hope it helps...
Dim sw As StreamWriter = New StreamWriter("Transactions.txt") Dim line As String
Try cn.Open() cnCmd.CommandText = "Select description, document, part_num, cur_qty, cond_code, generic1, generic2 from tblinventory" cnCmd.Connection = cn drDB = cnCmd.ExecuteReader
Dim description As String, document As String Dim part_num As String, cur_qty As String Dim cond_code As String, generic1 As String Dim generic2 As String
While drDB.Read description = drDB.Item("description") document = drDB.Item("document") part_num = drDB.Item("part_num") cur_qty = drDB.Item("cur_qty") cond_code = drDB.Item("cond_code") generic1 = drDB.Item("generic1") generic2 = drDB.Item("generic2")
line = description.PadRight(30) & document.PadRight(25) & "LINE," _ & part_num.PadLeft(10, "0") & ",RIC,UI," & cur_qty.padleft(5, "0") _ & "," & cond_code.PadRight(3) & "," & document.PadRight(25) _ & "," & generic1.PadRight(20) & "," & generic2.PadRight(20)
sw.WriteLine(line) ' change padding parameter accordingly! End While drDB.Close()
cn.Close()
|
|
|
|