Solved

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

Posted on 2006-06-25
6
170 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 142

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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16979055
FileCopy "file.dat" ,"file.bak"
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now