[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

fixed length

Posted on 2014-04-11
6
Medium Priority
?
222 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
[X]
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
  • 3
  • 2
6 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1332 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 1332 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

649 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