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??:confused:
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!!!
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.
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)
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
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
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
Re: How to change a string in a text file...
Quote:
Originally Posted by
jmsrickland
I cannot imagine what those PrivateProfile APIs have to do with the OP's request
Quote:
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.. :o
Quote:
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..
Re: How to change a string in a text file...
I was beginning to wonder
Re: How to change a string in a text file...
Quote:
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.
Re: How to change a string in a text file...
Quote:
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?
Re: How to change a string in a text file...
Quote:
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 :p but in this case simpler is the right choice..
Re: How to change a string in a text file...
Quote:
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