Valhalla Legends Forums Archive | Visual Basic Programming | Adding Text To End Of File

AuthorMessageTime
Ratio
[code]
Public Sub AppendFile()
Open App.Path & "\File.txt" For #1 As Append
Print #1,"Text To Add"
Close #1
End Sub
[/code]
August 16, 2006, 12:13 AM
Topaz
You're cool now
August 16, 2006, 12:41 AM
Ratio
...
August 16, 2006, 1:01 AM
Explicit[nK]
The thing is, people can write stuff like this in their sleep. Nothing personal.
August 16, 2006, 2:22 AM
Ratio
lol I was aware of this but you never know
August 16, 2006, 3:05 AM
Grok
[code]
    Dim fso As Object, TS As Object
    Dim ForAppending As Integer
    ForAppending = 8
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set TS = fso.OpenTextFile(fso.BuildPath(App.Path, "File.txt"), ForAppending, True)
    TS.WriteLine "Text to add"
    TS.Close
[/code]
August 16, 2006, 9:38 PM
HeRo
[quote author=Topaz link=topic=15545.msg156799#msg156799 date=1155688885]
You're cool now
[/quote]
[img]http://img154.imageshack.us/img154/695/osen2o7mp.jpg[/img]
August 17, 2006, 12:02 AM
FrOzeN
[quote author=Grok link=topic=15545.msg156832#msg156832 date=1155764288]
[code]
    Dim fso As Object, TS As Object
    Dim ForAppending As Integer
    ForAppending = 8
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set TS = fso.OpenTextFile(fso.BuildPath(App.Path, "File.txt"), ForAppending, True)
    TS.WriteLine "Text to add"
    TS.Close
[/code]
[/quote]
Using the FileSystemObject to append to files is pointless, it's slow and should really only be used in scripting when you don't have access to other methods of file handling.
August 17, 2006, 7:00 AM
Grok
Not ponitless.  The code in the OP was problematic.  Using the FSO reduces the number of things the beginning programmer can do wrong.  It was posted to show an alternative technology to the Open/Read/Write/Print/Close statements, for the beginner that is the same target audience as the OP.
August 17, 2006, 7:26 AM
TehUser
Furthermore, the FileSystemObjects provide a much better interface for manipulation.  Yes, there is a penalty in terms of the amount of lines that need to be executed, but it provides much better functionality (and much more reliable functionality) for a beginning programmer than attempting to use a different method like VB's Open or the Windows API.  And Frozen, if you hadn't noticed, the additional time that the scripting objects take on today's computers is negligible.

Edit: Forgot to mention that Ratio's code was buggy.  The line should be:[code]Open App.Path & "\File.txt" For Append As #1[/code][quote author=Ratio link=topic=15545.msg156798#msg156798 date=1155687222]
[code]
<snip>
Open App.Path & "\File.txt" For #1 As Append
<snip>
[/code]
[/quote]

But just for fun:

[code]Starting file open/close inclusive test.
Open at 1 rounds: 456.761962763424 microseconds
Approximate time per round: 456.761962763424 microseconds
FSO at 1 rounds: 1731.78434689325 microseconds
Approximate time per round: 1731.78434689325 microseconds

Starting file open/close exclusive test.
Open at 1 rounds: 5.86666741164031 microseconds
Approximate time per round: 5.86666741164031 microseconds
FSO at 1 rounds: 44.9777834892423 microseconds
Approximate time per round: 44.9777834892423 microseconds


Starting file open/close inclusive test.
Open at 10 rounds: 1335.64461405011 microseconds
Approximate time per round: 133.564461405011 microseconds
FSO at 10 rounds: 2385.21935050404 microseconds
Approximate time per round: 238.521935050404 microseconds

Starting file open/close exclusive test.
Open at 10 rounds: 12.8507952826407 microseconds
Approximate time per round: 1.28507952826407 microseconds
FSO at 10 rounds: 82.4127088778043 microseconds
Approximate time per round: 8.24127088778043 microseconds


Starting file open/close inclusive test.
Open at 100 rounds: 7783.11209944281 microseconds
Approximate time per round: 77.8311209944281 microseconds
FSO at 100 rounds: 14988.4971413965 microseconds
Approximate time per round: 149.884971413965 microseconds

Starting file open/close exclusive test.
Open at 100 rounds: 315.682579769216 microseconds
Approximate time per round: 3.15682579769216 microseconds
FSO at 100 rounds: 428.266721049742 microseconds
Approximate time per round: 4.28266721049742 microseconds


Starting file open/close inclusive test.
Open at 1000 rounds: 88384.1382075096 microseconds
Approximate time per round: 88.3841382075096 microseconds
FSO at 1000 rounds: 140828.513121081 microseconds
Approximate time per round: 140.828513121081 microseconds

Starting file open/close exclusive test.
Open at 1000 rounds: 1148.4699871073 microseconds
Approximate time per round: 1.1484699871073 microseconds
FSO at 1000 rounds: 4851.45458431169 microseconds
Approximate time per round: 4.85145458431169 microseconds


Starting file open/close inclusive test.
Open at 10000 rounds: 915901.043289021 microseconds
Approximate time per round: 91.5901043289021 microseconds
FSO at 10000 rounds: 1353676.79411769 microseconds
Approximate time per round: 135.367679411769 microseconds

Starting file open/close exclusive test.
Open at 10000 rounds: 9440.58532578861 microseconds
Approximate time per round: 0.944058532578861 microseconds
FSO at 10000 rounds: 43084.2467408567 microseconds
Approximate time per round: 4.30842467408567 microseconds


Starting file open/close inclusive test.
Open at 100000 rounds: 8818598.17379024 microseconds
Approximate time per round: 88.1859817379024 microseconds
FSO at 100000 rounds: 15551829.9113435 microseconds
Approximate time per round: 155.518299113435 microseconds

Starting file open/close exclusive test.
Open at 100000 rounds: 97634.4758900922 microseconds
Approximate time per round: 0.976344758900922 microseconds
FSO at 100000 rounds: 437273.452352184 microseconds
Approximate time per round: 4.37273452352184 microseconds[/code]
August 17, 2006, 2:28 PM
Ratio
:/ all my coding is runtimeage :/
August 29, 2006, 1:37 AM

Search