Solved

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

Posted on 2006-06-25
6
179 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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 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…
Suggested Courses
Course of the Month5 days, 15 hours left to enroll

626 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