Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

fixed length

Posted on 2014-04-11
6
198 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

840 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