Write to an existing file without overwriting what is in it using VB6?
I need to write to a text file to record a set of scores. Every time I write to the text file, it overwrites what was originally in the file. Can someone tell me how to not let it o开发者_开发技巧verwrite what is there or how to make it start writing in an empty space?
Open the file 'for append'.
This will erase the file:
Open "C:\path\to\file.txt" For Output As FILE
This will not erase the existing content:
Open "C:\path\to\file.txt" For Append As FILE
Use text append for this
Normally append text
'Start append text to file
FileNum = FreeFile
Open "D:\45.txt" For Append As FileNum
Print #FileNum, Text1.Text
Close FileNum
'End
Saving to app path
'Start append text to file
FileNum = FreeFile
Open App.Path & "\45.txt" For Append As FileNum
Print #FileNum, Text1.Text
Close FileNum
'End
VB6's file manipulation sucks. If you have to read a file and immediately write the data out (that is not appended) in the same loop for a conversion/some sort of manipulation. Open the original file, manipulate it but write to a "new" file with a different name in the same directory. At the end of the process, you save the new file, delete the original and then rename the new file to the old.
Dim path as String = "S:\My Documents\New folder (2)"
Dim filename as string = "\test.txt"
'To write into a file that will erase text:
FileOpen(1, path & filename, OpenMode.Output)
'To write into a file that will add to text:
FileOpen(1, path & filename, OpenMode.Append)
精彩评论