eneate
asked on
How do I append text to a text file, over writing a specific line,
Hi
I need tobe able to write a line to a text file overwriting the third line. I have used append but this just adds a line at the bottom of the file rather then over a specific line. I am using
Open file For Append As #1
Print #1, value
Close #1
I need tobe able to write a line to a text file overwriting the third line. I have used append but this just adds a line at the bottom of the file rather then over a specific line. I am using
Open file For Append As #1
Print #1, value
Close #1
ASKER
Thanks
How would I write the code to open the file and re save it as a new name, sorry this sounds really simple but I just can't quite find the right combination
How would I write the code to open the file and re save it as a new name, sorry this sounds really simple but I just can't quite find the right combination
FileCopy "file.dat" ,"file.bak"
ASKER
Hi
I've tried the following
Dim intInput As Integer
Dim intOutput As Integer
Dim intLine As Integer
Dim strLine As String
Const dcs_file As String = "c:\ipwin5\DCS-3.ini"
Dim value As String
value = "Veridata value: 0"
intInput = FreeFile()
intOutput = FreeFile()
If FileExists(dcs_file) Then
FileCopy "c:\ipwin5\DCS-3.ini", "c:\ipwin5\DCS-3.bak"
Open "c:\ipwin5\DCS-3.bak" For Input As #intInput
'Open "c:\ipwin5\DCS-3.ini" For Output As #intOutput I've tried commenting this out as I get file already open.
While Not EOF(intInput)
Line Input #intInput, strLine
intLine = intLine + 1
If intLine = 3 Then
Print #intOutput, value
Else
Print #intOutput, strLine
End If
Wend
Close #intInput
Close #intOutput
End If
As noted as bove I get file already open,if I comment it out at line print #output,strline I get error 54
bad file mode
Any ideas - thanks for the input.
I've tried the following
Dim intInput As Integer
Dim intOutput As Integer
Dim intLine As Integer
Dim strLine As String
Const dcs_file As String = "c:\ipwin5\DCS-3.ini"
Dim value As String
value = "Veridata value: 0"
intInput = FreeFile()
intOutput = FreeFile()
If FileExists(dcs_file) Then
FileCopy "c:\ipwin5\DCS-3.ini", "c:\ipwin5\DCS-3.bak"
Open "c:\ipwin5\DCS-3.bak" For Input As #intInput
'Open "c:\ipwin5\DCS-3.ini" For Output As #intOutput I've tried commenting this out as I get file already open.
While Not EOF(intInput)
Line Input #intInput, strLine
intLine = intLine + 1
If intLine = 3 Then
Print #intOutput, value
Else
Print #intOutput, strLine
End If
Wend
Close #intInput
Close #intOutput
End If
As noted as bove I get file already open,if I comment it out at line print #output,strline I get error 54
bad file mode
Any ideas - thanks for the input.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That's ok I've found another way round it, but thanks for the feedback. I;ll sort the points.
you have to read the entire file, and replace the third line by the new contents.
only if all the lines have EXACTLY the same width you could open the file in binary mode, go to the specific record, and put the new contents.
in your case, I would copy the file first (to .bak), then rewrite the file using code like this:
dim intInput as integer
dim intOutput as integer
dim intLine as integer
dim strLine as string
intInput = FreeFile()
intOutput = FreeFile()
open "file.bak" for input as #intInput
open "file.dat" for output as #intOutput
while not eof(intInput)
line input #intInput, strLine
intLine = intLine + 1
if intLine = 3 then
print #intOutput, Value
else
print #intOutput, strLine
end if
wend
close #intInput
clost #intOutput