Solved

fixed length

Posted on 2014-04-11
6
197 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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