Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to a specific position in a text file 1

Status
Not open for further replies.

chriscj21

Technical User
Mar 27, 2004
246
GB
All:

Hope someone can be of assistance.....

I am writing an application which needs to write to a specific location in a text file....

It is easy enough READING from a specific location (.readtoend, then creating an array, etc etc).....Is there any way I can do a similar thing for writing to a text file?

Failing that, is there anyway I can have a file open for reading and wrinting at same time?



Any help much appreciated






ChrisCj21
MCSE, A+, N+
 
A location in a text file is relative. To have location (x + 2), location (x+1) must exist (even as spaces).

Read the whole text, overwrite certain portions, and save as the same file.

You cannot have a writer and a reader simultaneously on a text file, as you would on an actual database.
 
What are you trying to accomplish? There may be a simple solution out there.
 
Thanks for help Dimandja

Ok - I need to configure VPN connections for clients....I have a streamreader to read from "RASPhone.PBK"

I have isolated any lines that have "IPAddress" in them....from here I need to write information back to those lines....Code as follows:


___________________________________________________

Dim sr As New StreamReader("C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\RASPhone.pbk")
Dim entirefile As String
Dim arr1 As Array
Dim i
Dim currentline As String
Dim CVVPN1, CVVPN2 As String
' currentline = sr.ReadLine
entirefile = sr.ReadToEnd
Dim sw As New StreamWriter("C:\IPLOG.txt", True)
arr1 = Split(entirefile, vbCrLf)
For i = 0 To UBound(arr1)
If InStr(arr1(i), "IpAddress") Then
sw.WriteLine(arr1(i))
If InStr(arr1(i), "255") Then
CVVPN1 = arr1(i)
MsgBox(arr1(i))
Else
If InStr(arr1(i), "254") Then
CVVPN2 = arr1(i)
MsgBox(arr1(i))
End If
End If
End If
Next
sr.Close()
sw.close





ChrisCj21
MCSE, A+, N+
 
To clarify.....

I have a formula which has calculated client's VPN IP's - the "RASPhone.pbk" file is on a base image and I need to write information back to this file for each location - based on the formula....

I have the file open for read access, I need a way to writeback to anyu location with "IPAddress" in it and change information.



thanks again


ChrisCj21
MCSE, A+, N+
 
Since the data you are writing colud be of a different size from the current data in the file, you should write to a separate file and then delete the original and rename your new file.

If the data was going to be exactly the same size you could treat the file as binary as opposed to text and write to a specific location. Unfortunately IP addresses vary in size.

Hope this helps.
 
Thanks Earthandfire....

So if I used some form of binaryreader I could read from a certain location?



Thanks


ChrisCj21
MCSE, A+, N+
 
No (that is earthandfire's point) the data could be a different size each time and therefore a binaryreader wouldn't work in this scenario.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Isnt this ideally suited for a string replace function?

Read the entire file into say a datatable which has one field for each row contents, then scan through it, doing a find & replace on IP addresses, then finally write the table back out.


Sweep
...if it works dont mess with it
 
This code works, whether the resulting text size is the same or not. Simply do your find and replace functions on the text saved from the file.

Code:
        Dim sr = New System.IO.StreamReader("C:\test.txt")
        entirefile = sr.ReadToEnd
        sr.close()
        sr = Nothing
        
        [COLOR=green]' Here, do your finds and replaces as you wish[/color]
        entirefile = Replace(entirefile, "IP 255", "IP 254")

        Dim sw = New System.IO.StreamWriter("C:\test.txt")
        sw.write(entirefile)
        sw.close()
        sw = Nothing
 
Have tried using replace and receive no errors but nothing seems to happen....

Here is full code:

___________________________________________________________
Dim sr As New StreamReader("C:\Documents and Settings\All Users\Application Data\Microsoft\Network\Connections\Pbk\RASPhone.pbk")
Dim entirefile As String
Dim arr1 As Array
Dim i
Dim CVVPN1, CVVPN2 As String
' currentline = sr.ReadLine
entirefile = sr.ReadToEnd
Dim sw As New StreamWriter("C:\IPLOG.txt", True)

arr1 = Split(entirefile, vbCrLf)

For i = 0 To UBound(arr1)
If InStr(arr1(i), "IpAddress") Then
sw.WriteLine(arr1(i))
If InStr(arr1(i), "255") Then
strch2 = Replace(arr1(i), "10.255.1.14", Label4.Text)
Else
If InStr(arr1(i), "254") Then
CVVPN1 = arr1(i)
Replace(arr1(i), "10.254.1.14", Label2.Text)
End If
End If
End If
Next
sr.Close()

sw.Close()

End Sub

___________________________________________________________

Any ideas where I am going wrong?






ChrisCj21
MCSE, A+, N+
 
You seem to:

1) Write the line out before you have done any replace functions
2) Just call the replace function but not set a variable equal to the value it produces. e.g.
Code:
Replace(arr1(i), "10.254.1.14", Label2.Text)
Rather than:
Code:
MyVariable = Replace(arr1(i), "10.254.1.14", Label2.Text)

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Chris

The following code wotks on the sample text file posted below. I called the file play.txt
------------------------
Line1
Line2
IpAddress=100.254.154.1
Line4
Line5
IpAddress=100.255.100.12
Line 6
Line7
------------------------

Code:
Dim sr As New StreamReader("c:\vbutils\PlayBits\play.txt")

Dim entirefile As String
entirefile = sr.ReadToEnd
sr.Close()

Dim sw As New StreamWriter("C:\vbutils\PlayBits\play1.txt", True)

Dim a As String()
a = Split(entirefile, vbCrLf)

Dim sIP1, sIP2 As String
sIP1 = "101.101.102.102"
sIP2 = "102.102.103.103"

Dim sFix As String
For i As Integer = 0 To a.Length - 1
    sFix = a(i)
    If a(i).IndexOf("IpAddress") >= 0 Then
        sFix = sFix.Replace("100.254.154.1", sIP1)
        sFix = sFix.Replace("100.255.100.12", sIP2)
    End If
    sw.WriteLine(sFix)
Next
sw.Close()


Sweep
...if it works dont mess with it
 
Use the debugger and note which lines get executed and which do not. Look at the values being processed.

Does anything get logged (IPLog.txt)?
Does "Replace" get executed?
 
Thanks for help Dimandja......

Kind of pick up .net as I go....

people like you help alot.


Much appreciated


Chris


ChrisCj21
MCSE, A+, N+
 
This now works fine....

Can I just ask one final question.....

Why in the code above to we use:

Dim a As String()
a = Split(entirefile, vbCrLf)

and not:

Dim a() as array
a = Split(entirefile, vbCrLf)


Only wondered as we know it will be an array so why not declare as one?



many thanks for getting this working



Chris


ChrisCj21
MCSE, A+, N+
 
Depending on how you declare your variables, you'll have access to specific methods and properties you can apply to your variable.

Read up on the String Class or the Array Class.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top