create .txt file

remya1000

Well-known member
Joined
Mar 29, 2007
Messages
122
Programming Experience
Beginner
i need to create a .txt file and create this table inside that.


Example:

VB.NET:
6/13/2007                            4:58:29 PM

Day Part           Count     TimeinSec     AVG
----------------------------------------------

06:00 - 11:30     4             91            00:22
11:30 - 13:30     10           70            22:22



during the runtime i need to take values from an XML file and do some calculations and create this table like this.

the code i used right now is this.
writeToFile(currDate & " " & currTime)
writeToFile("Day Part CNT SV Time AVG")
writeToFile("------------------------------------------")
writeToFile("06:00 - 11:30 " & Count & " " & Time & " " & Avg)
writeToFile("11:30 - 13:30 " & Count1 & " " & Time1 & " " & Avg1)

is this the correct way to display in that format. how can we give space.
"Day Part Count" here some space is there between DAYPART and COUNT. how can we give that space to write to a file.
is the code i gave is correct or now.

if you have anyidea how to do this please let me know.
thanks in advance.
 
Instead of giving the space i need between each word in same line like this,

writeToFile("06:00 - 11:30 " & Count & " " & Time & " " & Avg)

anyother way is there to tell first dispaly "DAYPART" then at this position display "COUNT" and then display "TIME" at this position and so on in same line.

how can i mention the space for each word to display.

if you have anyidea please let me know.
thanks in advanace.
 
Im not realy sure what you mean, but if you read around at
http://msdn2.microsoft.com/en-us/library/f63200h0.aspx

Maybe you find something there..

What you can also do. Its proboably very overpower, and rather retarded to do, but its mostly the only way i know to make the spaces between the same..

You just count the length of the Count and Time
Like:
Count.length
if count.length > 8 < 9 then writeToFile("06:00 - 11:30 " & Count & " " & Time & " " & Avg)
else if count.length > 9 < 10 then writeToFile("06:00 - 11:30 " & Count & " " & Time & " " & Avg)

etc etc.. Don't know if i made it right for your needs. If you change the spaces between etc, you can get it to fit, but you need to use a font that all char's are alike. that they all take the same space.. I don't remember any fonts like that atm..
 
First you would need to write the header info to the file which looks like it's already done

then for the actual data in the columns simply calculate the values needed inside a loop for each one and write out each line

then when the loop is done simply write the footer info then close the file

you of course will want to account for the space used by each piece of data in each column (so the first letter of each column lines up and looks organized) this is done simply by allowing a certain number of characters for each column and subtracting the one's used and filling the rest in with spaces.

there's a number of different ways to calculate that
 
Id do something like this:


VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim i As Integer
        Dim str As String
        'Dim lol As String

        Dim count, time, avg, count1, time1, avg1 As Integer
        count = 100
        time = 20
        avg = 1

        str = Date.Now & "                   " & Date.Now.Hour & ":" & Date.Now.Minute & vbNewLine
        str &= "Day Part         Count     Time    AVG" & vbNewLine
        str &= "------------------------------------------" & vbNewLine
        str &= "06:00 - 11:30  " & lol(count) & count & lol(time) & time & lol(avg) & avg & vbNewLine
        str &= "11:30 - 13:30  " & lol(count1) & count1 & lol(time1) & time1 & lol(avg1) & avg1


        myStream.write(str)
        myStream.close()

    End Sub


    Private Function lol(ByVal e As Integer)

        Dim strr As String
        If e.ToString.Length = 1 Then
            strr = "      "
        ElseIf e.ToString.Length = 2 Then
            strr = "     "
        ElseIf e.ToString.Length = 3 Then
            strr = "    "
        End If

        Return strr

    End Function

It should work then.. Tried to copy the .txt results, but the forum didnt take the spaces.. So just try it urself..

retrying with a new post..
 
Last edited:
Txt results..


VB.NET:
6/14/2007 8:16:46 PM                   20:16
Day Part         Count     Time    AVG
------------------------------------------
06:00 - 11:30      100     20      1
11:30 - 13:30        0      0      0

You just have to adjust it a bit, so it goes under the text aswell :p didnt bother to do that
 
one example with aligned string formatting below, see help page for Formatting Overview and especially Composite Formatting for more help on formatting pattern strings.
VB.NET:
Sub WriteAlignedData()
    Dim sw As New IO.StreamWriter("filename.txt")
    Dim d As Date = Date.Now
    sw.WriteLine("{0,-35}{1,10}", d.ToShortDateString, d.ToShortTimeString)
    sw.WriteLine()
    Dim stringformat As String = "{0,-15}{1,10}{2,10}{3,10}"
    sw.WriteLine(stringformat, "Day Part", "Count", "Time", "AVG")
    sw.WriteLine(New String("-", 45))
    Dim count As Integer = 1
    Dim time As Integer = 91
    sw.WriteLine(stringformat, "06:00 - 11:30", count, time, "12:22")
    sw.WriteLine(stringformat, "11:30 - 13:30", count + 1, time + 1, "22:22")
    sw.Close()
End Sub
 
Made a little function that might do the trick, it works for me, but I just used some easy values to test it with..
can you test it for me? and see if it works good for you..
Currently at work so got rather bad time :p


VB.NET:
    Private Function Space(ByVal e As String)
        Dim i As Integer
        Dim str As String
        Dim spaces As String = "                         "
        Dim test As Integer

        test = spaces.Length - (e.Length) - e.Length

        For i = 0 To 10
            If e.ToString.Length = i Then
                str = spaces.Substring(0, test)
                Return str
            End If
        Next

    End Function

Thats the function, to get it to work, do like i did with the test:
VB.NET:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim i As Integer
        Dim kk As String
        Dim ss As String
        Dim ll As String


        For i = 0 To 20
            kk = "KK" & i * 10
            lol(i) = kk & Space(kk)
            ss = "SS" & i * 100
            lol(i) &= ss & Space(ss)
            ll = "LL" & i * 100
            lol(i) &= ll & vbNewLine
        Next


        For i = 0 To 20
            TextBox1.Text &= lol(i)
        Next



    End Sub

It will write it out like this:
VB.NET:
KK0                   SS0                  LL0
KK10                 SS100               LL100
KK20                 SS200               LL200
KK30                 SS300               LL300
KK40                 SS400               LL400
KK50                 SS500               LL500
KK60                 SS600               LL600
KK70                 SS700               LL700
KK80                 SS800               LL800
KK90                 SS900               LL900
KK100               SS1000             LL1000
KK110               SS1100             LL1100
KK120               SS1200             LL1200
KK130               SS1300             LL1300
KK140               SS1400             LL1400
KK150               SS1500             LL1500
KK160               SS1600             LL1600
KK170               SS1700             LL1700
KK180               SS1800             LL1800
KK190               SS1900             LL1900
KK200               SS2000             LL2000

Seems the forum does not want to make the result thing i posted right..
Well, everything is left aligned, so all the KK SS LL etc come at theyr own row..

Don't know if it will work for you, but you might be able to change around, and see if you can get it to work for your current problem. The only thing you need is the function itself i guess, just place more spaces / less spaces on the spaces variable if it does not fit with the text you got over..
 
Levu, New String(" ", numberofspaces)
 
Levu, it's one of the constructor overloads for String class.
 
heay...... its working..........
thanks a lot Levu and John. i just tried both of your's codes and both works as i needed. and both the codes really good.
and its alligning properly under each heading. and it will arrange the space properly. as i did, if any of the work is bigger then the remaining works will move towards right. so its not proper.
but now its so good.
thanks a lot for both of you.... really thanks a lots.... hat's off to both of you guys.... good job....

the output is this:

6/13/2007 4:58:29 PM

Day Part Count Time AVG
------------------------------------------

06:00 - 11:30 4 91 12:22

11:30 - 13:30 0 0 22:22


once again thanks for you guys...

its not taking the space in forum. sorry.
 
as i did, if any of the work is bigger then the remaining works will move towards right. so its not proper.
You have make sure the alignment of each column fits - either check max field length manually and hardcode the values (as the code example) - or make two roundtrips through your data in code, one to find the max length field of each column, then one to actually output the data, the "trick" is then to create the formatting string dynamically to make the correct spacing. For example you had this formatting:
VB.NET:
Dim stringformat As String = "{0,-15}{1,10}{2,10}{3,10}"
After analyzing your data (first roundtrip) you end up with these field lengths:
DayPartMax=15, CountMax=10, TimeMax=10, AVGMax=10
Let's say these numbers also include 1 space you need between each column, you then end up with this dynamic formatting:
VB.NET:
Dim stringformat As String = String.Format("{{0,-{0}}}{{1,{1}}}{{2,{2}}}{{3,{3}}}", DayPartMax, CountMax, TimeMax, AVGMax)
Notice the same "field" technique as before is used, but now the fields {} we really want have to be escaped by doubling these chars {{}}.
When applying the result stringformat when doing the second roundtrip through the data and outputting it will be correctly aligned to any dynamic data.
 
Back
Top