Solved

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

Posted on 2006-06-25
6
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

710 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