Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I append text to a text file, over writing a specific line,

Posted on 2006-06-25
6
Medium Priority
?
191 Views
Last Modified: 2010-04-07
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
0
Comment
Question by:eneate
  • 3
  • 3
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16978938
>I need tobe able to write a line to a text file overwriting the third line.
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

0
 

Author Comment

by:eneate
ID: 16979047
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
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16979055
FileCopy "file.dat" ,"file.bak"
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:eneate
ID: 16979210
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.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 16979422
>'Open "c:\ipwin5\DCS-3.ini" For Output As #intOutput    I've tried commenting this out as I get file already open.
well, then you have some other process locking the file.
if another process locks the file, you cannot write to it. no way.
0
 

Author Comment

by:eneate
ID: 16979432
That's ok I've found another way round it, but thanks for the feedback. I;ll sort the points.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question