How could my program make like "C:File.txt"?
Printable View
How could my program make like "C:File.txt"?
VB Code:
Private Sub Command1_Click() Open "C:\rattata.txt" For Output As #1 Print #1, "test" 'exclude quotes Write #1, "test" 'include quotes Close #1 End Sub
should do itVB Code:
Open "C:\File.txt" For Output as #1 Print#1, "" Close #1
To write to a text file (this creates it if it does not exist already):
VB Code:
Dim FileHandle As Integer Open "c:\file.txt" For Output As #FileHandle Print #FileHandle, "Testing" Close #FileHandle
Oh, and if you want to read it back in ;):
VB Code:
Dim FileHandle As Integer Dim Contents As String, Cache As String Open "c:\file.txt" For Input As #FileHandle Line Input #1, Cache Contents = Contents & Cache & vbCrLf Close #FileHandle Contents = Right$(Contents, Len(Contents) - 1) ' get rid of trailing newline
:)
I'm sure theres a conspiracy going on here :D
I gave the most code, I want a cookie. :D
yes kind a scary :eek: :)
Hey you guys forgot about the filesystemobject! :D
VB Code:
Option Explicit Dim fso As New FileSystemObject Private Sub Form_Load() Dim tsr As TextStream Dim myFSOText As String myFSOText = "d:\anypath\text.txt" If fso.FileExists(myFSOText) = False Then Set tsr = fso.CreateTextFile(myFSOText) Else Set tsr = fso.OpenTextFile(myFSOText, ForAppending) End If tsr.Write "blah" tsr.Close Set tsr = Nothing End Sub
:D :D :D
regards,
Helger
Nooooooooooooo The FSO people found the thread!!! Were all dooooooomed!!
:D
All that code, and 3 lines of intrinsic VB code will do!!!! And it's FSO!!! :DQuote:
Originally posted by Helger
Hey you guys forgot about the filesystemobject! :D
:D :D :D
regards,
Helger
*diplomatic mode* I guess its always good to have alternatives *end diplomatic mode* The FSO sucks. :D
:D I knew you would love this! Very welcome!:D
Besides the serious one of getting the freefile didnt u also forget to declare the FileHandle as a VARIANT!! :pQuote:
Originally posted by filburt1
To write to a text file (this creates it if it does not exist already):
VB Code:
Dim FileHandle As Integer Open "c:\file.txt" For Output As #FileHandle Print #FileHandle, "Testing" Close #FileHandle
RegardsVB Code:
Dim FileHandle As Variant 'Only for Filburt's sake. S/be integer or long [b]FileHandle = FreeFile[/b] Open "c:\file.txt" For Output As #FileHandle Print #FileHandle, "Testing" Close #FileHandle
Stuart
So it'll typecast it then into something far more superior than Variant. :)
Here's a quick tip nobody mentioned before:
If you're using Print to put the text into the file like this:
Print #1, MyString
..and you're wondering why there are two extra characters at the end of the file, try adding a semicolon to the end of the line:
Print #1, MyString;
The semicolon prevents VB from adding a new line to the end of the file.