Really WEIRD Strings n Things

  • 13 years ago

     Hi all im Domino.vbcoder

    I am currently writing a security logger for a client, part of the project reads a txt file and searches for a line then replaces the line with a specified string as the code shows bellow, my problem is when it replaces the line with the specified string it some how leaves behind part of the line that was there previously.  I have tested the app vigoruosly to find the problem but no luck.  So to recap for example i am replaceing a line that starts with  'NUCLEAR=\ssmsilo191\secure access\hal9000' with a string that is 'NUCLEAR=\sdrsillo252\access secure trunk119\sepi2100' and im getting a resulting string of NUCLEAR=\sdrsillo252\access secure trunk119\sepi2100\hal9000, the strings that are concatenated are all as they should be. Can anyone help with another way of replaceing the string in an overhead friendly way ? plzz post code if possibble this is very important.

    Many Thanks
    Domino.vbcoder

    Private Sub textwritetest()
            Dim strstring2 As String = strpathforcurrent.ToUpper & "\" & gstrprojectname.ToUpper & "\" & gstrojobname.ToUpper

            Dim str As String
            Dim Fs As FileStream = New FileStream(strpathforcurrent & "\data.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
            Dim sw As New StreamWriter(Fs)
            Dim sr As New StreamReader(Fs)
            str = sr.ReadToEnd()


            str = str.Replace("NUCLEAR=" & gstrprojectfromcurrent, "NUCLEAR=" & strpathforcurrent.ToUpper & "\" & gstrprojectname.ToUpper)


            str = str.Replace("NUCLEAR=" & strforward, "NUCLEAR=" & strstring2)


            Fs.Position = 0
            Fs.SetLength(str.Length)
            sw.Write(str)
            sw.Flush()
            sw.Close()
            Fs.Close()

            
        End Sub

  • 13 years ago

    Looking at the code, it is not clear what strForward contains.

    But you have 2 search/replace things going on here,  and I think the second one is causing the problem.

    First time around, you replace

    NUCLEAR=\ssmsilo191\    ...(hal9000)

    by NUCLEAR=\ssmsilo191\secure access\  ...(hal9000)

    but that means the string is now actually:

    'NUCLEAR=\ssmsilo191\secure access\hal9000

     

    then you replace strForward (which I am assuming holds

    'NUCLEAR=\ssmsilo191\secure access)

    by 'NUCLEAR=\ssmsilo191\secure access\sepi2100\

    and that  gives you 'NUCLEAR=\ssmsilo191\secure access\sepi2100\hal9000

     

    Why not just find the row that says

    NUCLEAR=\ssmsilo191\

    and do the whole thing in one shot?

     

    Alternatively, try reading the file in one line at a time, processing each line, and writing it back out a line at a time to a new file?

     

  • 13 years ago

     Use dynamic replace within strings namespace : str = strings.replace(str, old, new) - difference is in using lenght replace / char by char replace

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“PHP is a minor evil perpetrated and created by incompetent amateurs, whereas Perl is a great and insidious evil perpetrated by skilled but perverted professionals.” - Jon Ribbens