Solved

VB 2005 Formatting Array Output Space Padding

Posted on 2007-11-19
12
175 Views
Last Modified: 2013-12-26
I am using VB 2005 Express. I have extracted data from one file and put it into another file. (.dat) My problem is the program that is reading the file needs it to be formatted in a specific way to be able to read it.

the first array is 13 spaces total, the second array is 8 spaces total, the third array is 20 spaces total and the last is 4 spaces total. Total meaning with data that array needs to be that amount of spaces.

My problem is the third array, this is where names are inputted and they can vary in length but the space can only accommodate 20 characters&spaces (total). All of the other inputs are pretty much constant.

Here is and example of what the output should look like:

123456      7890    johnjacob jinglehime1234
123456      7890   John Doe                  1234                    

Hopefully that gives you an idea of what I am dealing with. Notice that in the first row the name was cut off...that's okay. In my current code the output looks like this and my program won't read it:

123456 7890 johnjacob jinglehimerschmidt1234
123456 7890 John Doe1234


Imports System.IO
 

Module Mod1

   Sub Main()

      Dim fIn As StreamReader

      Dim fOut As StreamWriter

      Dim sArry() As String
 

'This reads file and creates/overwrites new .dat

      fIn = New StreamReader(New FileStream("c:\testfile1.dat", fileMode.Open))

      fOut = New StreamWriter(New FileStream("c:\testfile2.dat", FileMode.Create))
 

'Parse text

      Do Until (fIn.EndOfStream)

        sArry = Split(fIn.ReadLine, ",")

        If UBound(sArry) = 8 Then _

fOut.WriteLine(sArry(4) & " " & sArry(2) & " " & sArry(1) & " " & sArry(0) & "TEXT")

      Loop

      fIn.Close()

      fOut.Close()

   End Sub
 

End Module

Open in new window

0
Comment
Question by:acerteng
  • 5
  • 4
  • 3
12 Comments
 
LVL 11

Expert Comment

by:DotNetThinker
ID: 20313838
how about this...
sArray(1).SubString(0, 20)

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20313874
Use String.PadRight():
http://msdn2.microsoft.com/en-us/library/34d75d7s.aspx

fOut.WriteLine(sArry(4).PadRight(13) & sArry(2).PadRight(8) & sArry(1).PadRight(20) & sArry(0).PadRight(4) & "TEXT")

Open in new window

0
 

Author Comment

by:acerteng
ID: 20314889
Idle_Mind so far so good...thank you for the PadRight...I knew there had to be something out there.  I cannot get sArry(0) space sArry(1) to combine then do PadRight(20).

It counts the space only including sArry(1)...hopefully that makes sense

Maybe this will help sArry(0) fName sArry(1) is lName.  Need to concatenate those two with a space between them to total 20 spaces...cutting off anything over 20 from sArry(1)

fName lName fill in space totaling 20

Thank you for your input.

Not sure if this (concatenating) is a separate incident or can be part of original question...I can put in another question if I need to and accept your solution.  Let me know.







0
 
LVL 11

Expert Comment

by:DotNetThinker
ID: 20314945
Dim name as string = sArry(0) & " " & sArry(1)
name = name.SubString(0, 20)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20314954
Gotcha...so you need to concat, pad, and then trunc.

I think something like this is what you are after:

    Dim fullName As String = sArry(0) & " " & sArry(1)
    fullName = fullName.PadRight(20).Substring(0, 20)

    fOut.WriteLine(sArry(4).PadRight(13) & sArry(2).PadRight(8) & fullName & "TEXT")
0
 

Author Comment

by:acerteng
ID: 20315185
Okay my limited coding knowledge reached it limits last week.  I tried putting the Dim in a couple of places.  Since the sArry() is not known until the first file is read I get an error message.  

I tried to put the Dim in a couple of other places to no avail.  Where in the code should I put the Dim?
Imports System.IO
 

Module Mod1

    Sub Main()

        Dim fIn As StreamReader

        Dim fOut As StreamWriter

        Dim sArry() As String

        Dim fullName As String = sArry(0) & " " & sArry(1)

        fullName = fullName.PadRight(20).Substring(0, 20)
 
 

        fIn = New StreamReader(New FileStream("c:\testfile1.dat", FileMode.Open))

        fOut = New StreamWriter(New FileStream("c:\testfile2.dat", FileMode.Create))

        Do Until (fIn.EndOfStream)

            sArry = Split(fIn.ReadLine, ",")

            If UBound(sArry) = 8 Then _

            fOut.WriteLine(sArry(4).PadRight(12) & sArry(2).PadRight(8) & fullName & "TEXT")

        Loop

        fIn.Close()

        fOut.Close()

    End Sub
 

End Module

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Expert Comment

by:DotNetThinker
ID: 20315213

Imports System.IO

 

Module Mod1

    Sub Main()

        Dim fIn As StreamReader

        Dim fOut As StreamWriter

        Dim sArry() As String

        Dim fullName As String

        fullName = fullName.PadRight(20).Substring(0, 20)

 

 

        fIn = New StreamReader(New FileStream("c:\testfile1.dat", FileMode.Open))

        fOut = New StreamWriter(New FileStream("c:\testfile2.dat", FileMode.Create))

        Do Until (fIn.EndOfStream)

            sArry = Split(fIn.ReadLine, ",")

            If UBound(sArry) = 8 Then

                fullName = sArry(0) & " " & sArry(1)

                fullName = fullName.SubString(0, 20)

                fOut.WriteLine(sArry(4).PadRight(12) & sArry(2).PadRight(8) & fullName & "TEXT")

            EndIf

        Loop

        fIn.Close()

        fOut.Close()

    End Sub

 

End Module

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 20315246

Imports System.IO

Module Mod1

    Sub Main()

        Dim fIn As StreamReader

        Dim fOut As StreamWriter

        Dim sArry() As String

        Dim fullName As String 

 

        fIn = New StreamReader(New FileStream("c:\testfile1.dat", FileMode.Open))

        fOut = New StreamWriter(New FileStream("c:\testfile2.dat", FileMode.Create))

        Do Until (fIn.EndOfStream)

            sArry = Split(fIn.ReadLine, ",")

            If UBound(sArry) = 8 Then 

                fullName = (sArry(0) & " " & sArry(1)).PadRight(20).Substring(0, 20)

                fOut.WriteLine(sArry(4).PadRight(12) & sArry(2).PadRight(8) & fullName & "TEXT")

            End If

        Loop

        fIn.Close()

        fOut.Close()

    End Sub

End Module

Open in new window

0
 

Author Comment

by:acerteng
ID: 20315250
Copied and pasted still comes up with

Warning      1      Variable 'fullName' is used before it has been assigned a value. A null reference exception could result at runtime.
0
 
LVL 11

Expert Comment

by:DotNetThinker
ID: 20315267
Dim fullName As String = ""
0
 

Author Comment

by:acerteng
ID: 20315274
Idle_Mind that worked...sorry my earlier post was in reference to DotNetThinker

Thank you both very much!!!!!
0
 
LVL 11

Expert Comment

by:DotNetThinker
ID: 20315281
sorry, i forgot to delete line 9
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

706 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

14 Experts available now in Live!

Get 1:1 Help Now