Results 1 to 3 of 3

Thread: overwrite values in a file

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2004
    Posts
    908

    overwrite values in a file

    How do I change t he code in such a way that the number(value) I store in a file is over write ...currently i only know how to write values into the file but dunno how to overwrite.

    :
    ...
    ...
    intcount=intcount +1
    Private cmfFile_Click()
    Dim intFNum As Integer
    intFNum= FreeFile
    Open "C:123.txt" For Output As # intFNum

    Write #intFNum,intcount;
    Close #intFNum
    ...

    the result would be :1,2,3,4...
    but i want the number to overwrite to that there is only an Incremented number each time in the file when the button is click...thanks!

  2. #2
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    I use FSO for writing, I would do this:

    VB Code:
    1. Dim fso, txtfile
    2.  
    3.  
    4. Private sub cmfFile_Click()
    5.  
    6.     Set fso = CreateObject("Scripting.FileSystemObject")
    7.     Set txtfile = fso.CreateTextFile("c:\123.txt", True)
    8.    
    9.     txtfile.WriteLine (intcount)
    10.     txtfile.Close
    11.  
    12. End sub

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    You cannot overwrite the contents when the file is open for sequential access. Two options to solve your problem.

    Delete the file before the Open statement and keep your existing code or

    Open the file in Binary mode and use Put instead of Write.

    VB Code:
    1. intcount=intcount +1
    2. Private cmfFile_Click()
    3. Dim intFNum As Integer
    4. intFNum= FreeFile
    5. Open "C:123.txt" For Binary As # intFNum
    6.  
    7. Put intFNum, ,intcount
    8. Close #intFNum

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