Results 1 to 4 of 4

Thread: Edit specific line of text in a file

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    2

    Edit specific line of text in a file

    I am trying to make the code edit the a line in a text file, but it ends up deleting all the text and add the line at the begging of the file.

    Code:
           
            Dim FILE_NAME As String = "config.conf"
            Dim i As String
            Dim Text(90) As String
    
    
            Text(90) = "        Address = " & Chr(34) & KryptonTextBox1.Text & Chr(34) & ""
    
            Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
    
            i = 90
            objWriter.WriteLine(Text(i))
    
            objWriter.Close()
    the line i want to edit is line number 90



    Thanks

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Edit specific line of text in a file

    Thats because you are opening a file for write access and only writing one line to it. You are declaring a string array with 91 elements in it- of which will all be empty except for the last line according to your code. I'd have thought you wanted to read the existing file into that array first.

    If you want to write all the lines to the file you'll need to loop through them all.

    Other things to note - you are declaring i as a string variable and assigning a number to it - integer would be a more obvious choice for the variable type, however if all you are doing is assigning a constant to it and using it in the next line then there's little point in having it at all - you could just as easily have the line "objWriter.WriteLine(Text(90))"

    Code:
            Dim FILE_NAME As String = "config.conf"
            Dim i As String
            Dim Text(90) As String
    
            'Insert your own code for reading the file into the array here
    
            Text(90) = "        Address = " & Chr(34) & KryptonTextBox1.Text & Chr(34) & ""
    
            Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
    
            for i as integer = 0 to 90
                objWriter.WriteLine(Text(i))
            next i
    
            objWriter.Close()
    Last edited by keystone_paul; Apr 30th, 2009 at 01:55 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    2

    Re: Edit specific line of text in a file

    Thanks for replying,
    as you probably figured, im pretty new to VB so i need some more help

    Code:
            Dim FILE_NAME As String = "config.conf"
    
    
            Dim fs As FileStream = File.OpenRead("config.conf")
            Dim sr As New StreamReader(fs)
            Dim test As String = sr.ReadToEnd()
            Dim full As String
    
            fs.Close()
    
    
    
            Dim text As String
            text = vbNewLine + "        Address = " & Chr(34) & KryptonTextBox1.Text & Chr(34) & ""
    
    
            full = test & text
            Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
    
    
            objWriter.WriteLine(full)
    
    
            objWriter.Close()
    That code adds the 'text' at the end of the file, i cant figure out how to edit line nr 90...
    the file contains 100 lines

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Edit specific line of text in a file

    OK the problem you have now is that you have read the entire file into a single string by using "sr.ReadToEnd()" which means all the lines are lumped together, and it is not simple to edit a specific position in there.

    You were along the right lines before with having an array. If you know that you have 100 lines you can dimension an array as you did in your first snippet, and then rather than using ReadToEnd you would have a loop that used sr.ReadLine to get a single line at a time either until you've hit your 100 lines, or the end of the file if you want to be on the safe side. Take a look at the MSDN documentation on the streamreader class if you want more information on this.

    The 2nd part of your question is a little tricky if taken at face value - when you say you want to "edit line nr 90" how are you going to work out which line you actually want? If you actually mean line 90 then your line from your original snippet was fine :

    Code:
    Text(90) = "        Address = " & Chr(34) & KryptonTextBox1.Text & Chr(34) & ""
    Except that as arrays are zero based, text(90) is actually the 91st row of the file.

    You would then want to write the file out, again using a loop and objWriter.WriteLine for each line in the array.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width