Solved

VB 2005 Formatting Array Output Space Padding

Posted on 2007-11-19
12
177 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 102
Convert VB6 MSXML2.ServerXMLHTTP process to C# 2 46
Sending Attachment via CDO 3 68
Formula problem with Excel attachment 6 26
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

806 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