Solved

fixed length

Posted on 2014-04-11
6
195 Views
Last Modified: 2014-05-03
I want to create a fixed length file.  My data will come from db. The max length on each line is 150. Am I on the right track?

 Public Sub Test()

     
        Dim fs As New FileStream("C:\Users\test\test.txt", FileMode.OpenOrCreate)
        Dim SW As New StreamWriter(fs, Encoding.UTF8)

        Dim output_A1 As String = Space(150)
        Dim output_A2 As String = Space(150)


        output_A1 = output_A1.Insert(0, "PIR")
        output_A1 = output_A1.Insert(3, "01")
        output_A1 = output_A1.Insert(140, "1111111111")

        SW.Write(RTrim(output_A1) & vbCrLf)
        output_A1 = ""

        output_A2 = output_A2.Insert(0, "PIR")
        output_A2 = output_A2.Insert(3, "02")
        output_A2 = output_A2.Insert(140, "2222222222")

        SW.Write(RTrim(output_A2) & vbCrLf)
        output_A2 = ""

        'etc

        SW.Close()


    End Sub

Open in new window

0
Comment
Question by:VBdotnet2005
  • 3
  • 2
6 Comments
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 333 total points
ID: 39995082
I suppose you could do it that way. Although, why not use the Write method of the StreamWriter class rather than insert into a temporary string?

e.g.

SW.Write("PIR")
SW.Write("01".PadRight(135))
SW.Write("1111111111")

SW.WriteLine()  ' Replaces SW.Write(RTrim(output_A1) & vbCrLf)

Open in new window

0
 

Author Comment

by:VBdotnet2005
ID: 39995101
If I just use sw.write, how does it know which position each string needs to go to? Each line max len is 150.
for example : accountnumber, name, id, date

accountnumber needs to be written at position 10 (the max len of account 12)
name needs to be written at position 30 (the max len of name 32)
id needs to be written at position 60 (the max len of id 4)
date needs to be written at position 50 (the max len of id 6) '041114

1234567890   john doe  1111 041114
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 333 total points
ID: 39995146
That's where the padding calls (see my use of PadRight above) come into play. Basically, you add a call to Write to execute the output of the string, but the string itself has a PadRight call appended to it. The value passed to PadRight should be the maximum width of that field--PadRight will subtract as necessary. So, in your example, for consistency:

SW.Write("PIR".PadRight(3, ' '))
SW.Write("01".PadRight(135, ' '))
SW.Write("1111111111".PadRight(10, ' '))

SW.WriteLine()  ' Replaces SW.Write(RTrim(output_A1) & vbCrLf)

Open in new window


It doesn't matter for "PIR" that you are using 3 with PadRight--"PIR" is already three characters in length, so PadRight will add nothing to the end of it. For "01", you have two characters, and you have the three characters to account for from "PIR". That totals 5 characters. In order for the next "1111111111" to start in the correct spot, you need to pad "01" with enough characters to equal 140--the starting point from your original sample. So, the 5 characters of "PIR" and "01" plus 135 padding characters will equal 140. Now you simply dump out "1111111111", adding (potentially unused) padding of 10 characters to give a grand total of 150.

** Note, I think I mistyped the function call in my first comment. It is missing a second parameter. The code in this comment has the correct usage.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39995153
P.S.

I haven't used any myself, but there are several libraries available for creating flat files. You might have a gander at the following:

http://sourceforge.net/projects/filehelpers/
https://www.nuget.org/packages/FlatFiles/0.1.8
0
 
LVL 27

Expert Comment

by:Ark
ID: 39995605
Can you elaborate a bit?
accountnumber needs to be written at position 10 (the max len of account 12)
name needs to be written at position 30 (the max len of name 32)
id needs to be written at position 60 (the max len of id 4)
date needs to be written at position 50 (the max len of id 6) '041114
In this case name will overwrite date and 2 chars of id
0
 
LVL 27

Accepted Solution

by:
Ark earned 167 total points
ID: 39995607
PS To work with existing file and edit it according DB changes look at SW.BaseStream.Seek method:
Dim lineOffset = recordNumber*150 'assuming recordNumber is 0-based
SW.BaseStream.Seek(lineOffset + 10,StreamOrigin.Begin) 'seek to accountNumber position
SW.Write(accountNumber.PadRight(12," "))
SW.Flush()
SW.BaseStream.Seek(lineOffset + 30,StreamOrigin.Begin)'seek to name position
SW.Write(name.PadRight(32," "))
SW.Flush()

Open in new window

Or using sub:
    Private Sub WriteData(ByVal sw As StreamWriter, ByVal data As String,
                          ByVal offset As Integer, ByVal length As String)
        If data.Length > length Then
            data = data.Substring(0, length)
        Else
            data = data.PadRight(length, " "c)
        End If
        sw.BaseStream.Seek(offset, SeekOrigin.Begin)
        sw.Write(data)
        sw.Flush()
    End Sub

Open in new window

Using:
WriteData(SW,accountNumber,lineOffset+10,12)
WriteData(SW,name,lineOffset+30,32)
'......
SW.WriteLine(); 'add vbcrlf
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now