Results 1 to 16 of 16

Thread: How do i make a txt files?

  1. #1

    Thread Starter
    Hyperactive Member BrandonTurner's Avatar
    Join Date
    Sep 2001
    Location
    East Lansing, Michiagn
    Posts
    268

    How do i make a txt files?

    How could my program make like "C:File.txt"?

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. Private Sub Command1_Click()
    2.     Open "C:\rattata.txt" For Output As #1
    3.     Print #1, "test"    'exclude quotes
    4.     Write #1, "test"    'include quotes
    5.     Close #1
    6. End Sub
    -= a peet post =-

  3. #3
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    VB Code:
    1. Open "C:\File.txt" For Output as #1
    2. Print#1, ""
    3. Close #1
    should do it

  4. #4
    To write to a text file (this creates it if it does not exist already):

    VB Code:
    1. Dim FileHandle As Integer
    2. Open "c:\file.txt" For Output As #FileHandle
    3. Print #FileHandle, "Testing"
    4. Close #FileHandle

  5. #5
    Oh, and if you want to read it back in :

    VB Code:
    1. Dim FileHandle As Integer
    2. Dim Contents As String, Cache As String
    3. Open "c:\file.txt" For Input As #FileHandle
    4. Line Input #1, Cache
    5. Contents = Contents & Cache & vbCrLf
    6. Close #FileHandle
    7. Contents = Right$(Contents, Len(Contents) - 1) ' get rid of trailing newline


  6. #6
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    I'm sure theres a conspiracy going on here

  7. #7
    I gave the most code, I want a cookie.

  8. #8
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    yes kind a scary
    -= a peet post =-

  9. #9
    Helger
    Guest
    Hey you guys forgot about the filesystemobject!

    VB Code:
    1. Option Explicit
    2. Dim fso As New FileSystemObject
    3. Private Sub Form_Load()
    4. Dim tsr As TextStream
    5. Dim myFSOText As String
    6.  
    7. myFSOText = "d:\anypath\text.txt"
    8.  
    9. If fso.FileExists(myFSOText) = False Then
    10.     Set tsr = fso.CreateTextFile(myFSOText)
    11. Else
    12.     Set tsr = fso.OpenTextFile(myFSOText, ForAppending)
    13. End If
    14. tsr.Write "blah"
    15.  
    16. tsr.Close
    17. Set tsr = Nothing
    18. End Sub



    regards,

    Helger

  10. #10
    Frenzied Member nishantp's Avatar
    Join Date
    Jan 2001
    Location
    Where you least expect me to be
    Posts
    1,375
    Nooooooooooooo The FSO people found the thread!!! Were all dooooooomed!!



































    You just proved that sig advertisements work.

  11. #11
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Originally posted by Helger
    Hey you guys forgot about the filesystemobject!


    regards,

    Helger
    All that code, and 3 lines of intrinsic VB code will do!!!! And it's FSO!!!

  12. #12
    *diplomatic mode* I guess its always good to have alternatives *end diplomatic mode* The FSO sucks.

  13. #13
    Helger
    Guest
    I knew you would love this! Very welcome!

  14. #14
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Originally posted by filburt1
    To write to a text file (this creates it if it does not exist already):
    VB Code:
    1. Dim FileHandle As Integer
    2. Open "c:\file.txt" For Output As #FileHandle
    3. Print #FileHandle, "Testing"
    4. Close #FileHandle
    Besides the serious one of getting the freefile didnt u also forget to declare the FileHandle as a VARIANT!!
    VB Code:
    1. Dim FileHandle As Variant 'Only for Filburt's sake. S/be integer or long
    2.     [b]FileHandle = FreeFile[/b]
    3.     Open "c:\file.txt" For Output As #FileHandle
    4.     Print #FileHandle, "Testing"
    5.     Close #FileHandle
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  15. #15
    So it'll typecast it then into something far more superior than Variant.

  16. #16
    Tygur
    Guest
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width