PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
How to change a string in a text file...-VBForums
Results 1 to 12 of 12

Thread: How to change a string in a text file...

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    9

    Unhappy 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!!!

  2. #2
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    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.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  3. #3
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    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.

  4. #4
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    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

  5. #5
    Addicted Member
    Join Date
    Mar 2009
    Posts
    195

    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:
    1. 'Private Declare Function WritePrivateProfileSection Lib "kernel32.dll" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    2. 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
    3. 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
    4. 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
    5. Private Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    6. 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

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    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.

  7. #7
    Addicted Member
    Join Date
    Mar 2009
    Posts
    195

    Re: How to change a string in a text file...

    Quote Originally Posted by jmsrickland View Post
    I cannot imagine what those PrivateProfile APIs have to do with the OP's request
    Quote Originally Posted by Mr_Bateman View Post
    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..

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    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.

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Re: How to change a string in a text file...

    Quote Originally Posted by SuperDre View Post
    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.

  10. #10
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: How to change a string in a text file...

    Quote Originally Posted by Mr_Bateman View Post
    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?

  11. #11
    Addicted Member
    Join Date
    Mar 2009
    Posts
    195

    Re: How to change a string in a text file...

    Quote Originally Posted by jmsrickland View Post
    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..

  12. #12
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Re: How to change a string in a text file...

    Quote Originally Posted by Doogle View Post
    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
  •  



Featured


Click Here to Expand Forum to Full Width