|
-
Jul 18th, 2012, 09:03 AM
#1
Thread Starter
New Member
How to change a string in a text file...
I have a user enter an IP address that is being written to a file in the form of
10.x.y.50
I need to make another IP address from the user input IP address to take the form of 10.(x+10).(y+15).50
For example,
If the user inputs the IP address as "10.20.30.50"
The text file should appear as...
IPAddress=10.20.30.50
IPAddress2=10.30.45.50
Any ideas how to do this??
Code:
Private Sub Save_Click()
Dim path As String
path = "C:\test.ini" ' The file to write to
Dim ss As String
ss = FreeFile
If FExist(path) Then
Open path For Output As #ss
Else
Open path For Output As #ss
End If
IP = IPAddr.Text
Print #ss, "IPAddress=" & IPAddress.Text 'Takes our user input IP
Print #ss, "IPAddress2=" 'HELP!!!
-
Jul 18th, 2012, 09:15 AM
#2
Re: How to change a string in a text file...
1. u can use Split() function to split the string delimited by "." (dot)
2. add 10 with 2nd value (mind the index value starts with 0)
3. add 15 with 3rd value
4. rejoin them and print into txt file.
-
Jul 18th, 2012, 04:47 PM
#3
Re: How to change a string in a text file...
I fear he will ask how to do that.
Taking seenu's steps given above:
Code:
Dim IP() As String
IP = Split(IPAddress.Text, ".")
IPAddress2.Text = IP(0) & "." & CStr(Val(IP(1)) + 10) & "." & CStr(Val(IP(2)) + 15) & "." & IP(3)
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jul 18th, 2012, 06:56 PM
#4
Re: How to change a string in a text file...
what is this suppose to do?
Code:
If FExist(path) Then
Open path For Output As #ss
Else
Open path For Output As #ss
End If
-
Jul 20th, 2012, 08:22 AM
#5
Hyperactive Member
Re: How to change a string in a text file...
Or use the PrivateProfile WinAPI routines which is the WinAPI which handles INI files:
Just replace Private Declare with Public Declare if you need to. There are propably some more, but the are the ones I have in a special class which I use for handling windowsregistry..
vb Code:
'Private Declare Function WritePrivateProfileSection Lib "kernel32.dll" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long Private Declare Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long Private Declare Function GetPrivateProfileInt Lib "kernel32.dll" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long Private Declare Function GetPrivateProfileSection Lib "kernel32.dll" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Private Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Private Declare Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
-
Jul 20th, 2012, 11:23 AM
#6
Re: How to change a string in a text file...
I cannot imagine what those PrivateProfile APIs have to do with the OP's request
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jul 20th, 2012, 01:06 PM
#7
Hyperactive Member
Re: How to change a string in a text file...
 Originally Posted by jmsrickland
I cannot imagine what those PrivateProfile APIs have to do with the OP's request
 Originally Posted by Mr_Bateman
I have a user enter an IP address that is being written to a file in the form of
10.x.y.50
The text file should appear as...
IPAddress=10.20.30.50
IPAddress2=10.30.45.50
which is exactly what you can do with the WritePrivateProfileString Is used for, writing and reading INI-kinda files, exactly what he's asking for..... but then using API..
Hmmm.. Sorry, now I see what he's actually asking.. 
I need to make another IP address from the user input IP address to take the form of 10.(x+10).(y+15).50
well, it was late at work and I was in a hurry to get out so I didn't read the question correctly..
But then the Split is the best simple option or just parse it with Instr(), but that's much more code..
-
Jul 20th, 2012, 02:12 PM
#8
Re: How to change a string in a text file...
I was beginning to wonder
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jul 20th, 2012, 02:15 PM
#9
Re: How to change a string in a text file...
 Originally Posted by SuperDre
But then the Split is the best simple option or just parse it with Instr(), but that's much more code..
How can you get any simpler than using Split?
Instr? Too much parsing involved.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Jul 20th, 2012, 02:52 PM
#10
Re: How to change a string in a text file...
 Originally Posted by Mr_Bateman
I need to make another IP address from the user input IP address to take the form of 10.(x+10).(y+15).50
Why on earth would you want to do this ?
If the user enters: '10.260.250.50' for example, what would you expect as the output?
-
Jul 20th, 2012, 02:53 PM
#11
Hyperactive Member
Re: How to change a string in a text file...
 Originally Posted by jmsrickland
How can you get any simpler than using Split?
Instr? Too much parsing involved.
You can't get any simpler than split but you can go much faster than but in this case simpler is the right choice..
-
Jul 20th, 2012, 02:57 PM
#12
Re: How to change a string in a text file...
 Originally Posted by Doogle
Why on earth would you want to do this ?
If the user enters: '10.260.250.50' for example, what would you expect as the output?
Kaboom!
If OP is smart enough he would insure that the second and third numbers are no greater than 245
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|