Solved

fixed length

Posted on 2014-04-11
6
199 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 75

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 75

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

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 28

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 28

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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