Solved

writing to a specific column in a text file

Posted on 2009-07-15
4
203 Views
Last Modified: 2013-11-26
I want to write to a text file.
I would want data to be written to specific columns because i will need to read it later with a data dictionary.

eg Staff id - starts at column 1
staff name - starts at column 10
staff salary - starts at column 50.

I am using vb6

regards

anthony
0
Comment
Question by:Anthony Matovu
4 Comments
 
LVL 15

Accepted Solution

by:
JackOfPH earned 250 total points
ID: 24867707
Here is a sample:
 Public Sub WruteToText()
 
 
        Dim Column1 As String = "Column1"
        Dim Column2 As String = "Column2"
        Dim Column3 As String = "Column3"
        Dim Column4 As String = "Column4"
        Dim Column5 As String = "Column5"
 
        Using textFile As IO.TextWriter = IO.File.CreateText("D:\MyTextFile.txt")
            'Set a delimeter for the textfile to separate the each column.
            'The delimeter for this example is ||.
            textFile.WriteLine("{0}||{1}||{2}||{3}||{5}", Column1, Column2, Column1, Column2, Column1)
            textFile.WriteLine("{0}||{1}||{2}||{3}||{5}", Column1, Column2, Column1, Column2, Column1)
 
            'Save the data to the textfile.
            textFile.Flush()
            textFile.Close()
        End Using
 
    End Sub
 
    Private Sub ReadText()
 
 
        Dim textFile As IO.TextReader = IO.File.OpenText("D:\MyTextFile.txt")
        Dim value As String
        Dim charge As String = ""
 
        Do
            'Read each line in the text file.
            value = textFile.ReadLine
 
            If value Is Nothing Then
                Exit Do
            End If
 
            'This will split the columns in the given line
            Dim Columns() As String = value.Split("||")
 
            'Read each column in the current line in the text file.
            For Each column As String In Columns
                MsgBox(column)
            Next
 
        Loop
 
        textFile.Close()
 
    End Sub

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24867920
@JackofPH
"I am using vb6" is the third last line!
0
 
LVL 32

Assisted Solution

by:Robberbaron (robr)
Robberbaron (robr) earned 250 total points
ID: 24868379
this function is from a while ago but still in my toolbox.... havent checked the method of formating strings recently.
  uses VB6 format function and pads front and back as necessary


for each line
   sOut = ""
   sOut = sOut & Fmt$(staffid,"     00000","R")   'staffid right justified in 10 char
   sOut = sOut & Fmt$(staffname,space$(40),"L")   'name left justified in 40 char
   sOut = sOut & Fmt$(salary,"###########0.00","R")   'salary in 15 char
 
print #1,sOut
 
next line
 
'-------------------------------------------
Function Fmt$(txt As String, field$, justification$)
    'formats txt in specified field  width
    Dim a As String, x As Integer, y As Integer, totlen As Integer
    
    'Determine length of field
    x = InStr(field$, ";")   'search for separator
    If x = 0 Then x = Len(field$) Else x = x - 1
    totlen = x
 
    a = Trim$(left$(Format(txt, field$), totlen))
    
    Select Case LCase$(justification$)
        Case "L"
            x = 0: y = totlen - Len(a)
        Case "R"
            x = totlen - Len(a): y = 0
        Case "C"
            x = (totlen - Len(a)) / 2
            y = totlen - Len(a) - x
        Case Else
            'use left
            x = 0: y = totlen - Len(a): y = 0
    End Select
 
    Fmt$ = Space$(x) + a + Space$(y)
 
End Function

Open in new window

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

Suggested Solutions

Title # Comments Views Activity
asp Google Map 2 66
TFS 2015 Access denied. 1 30
Asp.Net Session Question 2 36
"lblTime is not declared" 3 38
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

697 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