• Welcome to Valhalla Legends Archive.
 

Adding Text To End Of File

Started by HardCoded., August 15, 2006, 07:13 PM

Previous topic - Next topic

HardCoded.


Public Sub AppendFile()
Open App.Path & "\File.txt" For #1 As Append
Print #1,"Text To Add"
Close #1
End Sub
ZOMG

topaz

RLY...?

HardCoded.

ZOMG

Explicit

The thing is, people can write stuff like this in their sleep. Nothing personal.
I'm awake in the infinite cold.

[13:41:45]<@Fapiko> Why is TehUser asking for wang pictures?
[13:42:03]<@TehUser> I wasn't asking for wang pictures, I was looking at them.
[13:47:40]<@TehUser> Mine's fairly short.

HardCoded.

lol I was aware of this but you never know
ZOMG

Grok


    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

Hero


FrOzeN

Quote from: Grok on August 16, 2006, 04:38 PM

    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

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.
~ FrOzeN

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.

TehUser

#9
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:Open App.Path & "\File.txt" For Append As #1
Quote from: Ratio on August 15, 2006, 07:13 PM

<snip>
Open App.Path & "\File.txt" For #1 As Append
<snip>


But just for fun:

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

HardCoded.

:/ all my coding is runtimeage :/
ZOMG