Solved

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

Posted on 2006-06-25
6
174 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
Industry Leaders: 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!

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
VBA error replacing data 6 41
passing parameter in sql procedure 9 68
How does CurrentUser work? 10 42
How to compare ms sql hashbytes results within vb6 5 97
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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

749 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