Results 1 to 5 of 5

Thread: text files

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506

    Talking text files

    simple i kno, and ive done it before but have forgot

    k.. ive got a text file and im usin my program to write to it, but everytime it writes to it, all the old content is gone! because im using the wrong method, but i dunno whats the right method :s heres what ive got atm:
    VB Code:
    1. Dim fso, textfile
    2. Set fso = CreateObject("Scripting.FileSystemObject")
    3. If fso.FileExists("c:\test.txt") = True Then
    4. Set textfile = fso.OpenTextFile("c:\test.txt", ForWriting)
    5. Else
    6. Set textfile = fso.CreateTextFile("c:\test.txt", True)
    7. End If
    help appreciated

  2. #2
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Why are you using FSO? You need to use Appent, not Writing. Why don't you just use Open "File" For Input/Output/Append?
    <removed by admin>

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506
    what else apart from fso can i use? :> + i just tried it with forappend and not alot happens

  4. #4
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    VB Code:
    1. 'if the file doesn't exist then create it
    2. If Len(Dir("C:\test.txt")) = 0 Then
    3.   Open "C:\test.txt" For Output as #1
    4.     Print #1, ""
    5.   Close #1
    6. 'if the file exists the append to it
    7. Else
    8.   Open "C:\test.txt" For Append As #1
    9.     Print #1, "your data goes here"
    10.   Close #1
    11. End If
    12.  
    13. 'read the file back into a variable:
    14. Open "C:\test.txt" For Input As #1
    15.   Dim Buffer As String
    16.   Buffer = Input$(LOF(1), 1)
    17. Close #1
    18. 'Buffer now contains the file
    <removed by admin>

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    You do not need to test if it exists. Append will create the file if it doesn't exist. On the other hand, if it does exist, it will write at the end of it.
    VB Code:
    1. Open "C:\test.txt" For Append As #1
    2.     Print #1, "your data goes here"
    3.   Close #1
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

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