CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2003
    Posts
    165

    deleting & writing in the same file

    i have a file info.ini

    which has a text like:

    ;comment
    [management=1]
    patid=34
    ;following fields not need it
    lastname=johnson
    firstname=tom

    [image]
    color=445


    the questin is how can i delete or replace the patid=34 with some other patid=2
    and
    lastname=thomas
    firstname=glen

    ?
    i tried to write over spaces,but the problem is that i cannot know exactly when is the end of line,and also the real problem is that end of line,i will have to overwrite
    with a much longer line,and it will go over next line,and it will be a mess.
    as i noticed ,this writing in a file work like the overwrite,not like the insert ,from the keyboard?

    i used something like this:
    Dim fs As New FileStream("c:\we.txt", FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)
    Dim fs1 As New FileStream("C:\we.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)
    Dim sr As StreamReader = New StreamReader(fs)
    Dim sw As New StreamWriter(fs1)
    Dim str As String
    Dim allText As String

    Do
    str = sr.ReadLine()
    allText = allText & str

    If str = "patid=34" Then
    Dim pos As Integer
    MessageBox.Show(allText)
    pos = allText.Length - str.Length
    MessageBox.Show(pos)
    fs1.Seek(pos, SeekOrigin.Begin)
    sw.Write(Space(20 * 256))
    fs1.Seek(pos, SeekOrigin.Begin)

    sw.WriteLine("patid=2")
    sw.WriteLine(";following fields not need it")
    sw.WriteLine("lastname=thomas")
    sw.writeline("firstname=glen")
    Exit do
    End If

    Loop Until str Is Nothing
    sw.Flush()

    sw.Close()

    sr.Close()
    fs.Close()
    MessageBox.Show("end")

    so i simulate the deleting,by inserting lots of spaces,but because of that \r\n it will be a mess.beacuse i want to overwrite again other information,with other names,and in file it will be lots of spaces,and that seek() reposition at the end of that file.and all this because the writting is like an overwrite not like an insert,and i'm not able to delete and insert.

  2. #2
    Join Date
    Dec 2002
    Posts
    305

    Re: deleting & writing in the same file

    Why don't you first open the file for input only, read and store each line in an array of strings. Close the file Change the line you want to change. Then open the file for output and write each line in the same order. Then save the file. Since .ini files are not that large, this should work very well.

  3. #3
    Join Date
    Jan 2003
    Posts
    165

    Re: deleting & writing in the same file

    Quote Originally Posted by Gizmo001
    Why don't you first open the file for input only, read and store each line in an array of strings. Close the file Change the line you want to change. Then open the file for output and write each line in the same order. Then save the file. Since .ini files are not that large, this should work very well.
    you don't understand that it does not matter when you write in the file,if you close and after that open to write,is the same,the ideea is the writing.if the new line is smaller that old line it will remain the rest of the old line because it will overwrite the string,do you understand that?

    try your solution,have a initial file,and replace with a smaller or larger string and you will see that it will be a mess the file after all this.

  4. #4
    Join Date
    Dec 2002
    Posts
    305

    Re: deleting & writing in the same file

    If what you say is true, then you can try this:

    after you have stored the information during file-read, close and delete the file. Then open a new file with the same name and write into that the stored and edited information.

  5. #5
    Join Date
    Dec 2002
    Posts
    305

    Re: deleting & writing in the same file

    In any case, you are wrong about strings being left over. Whenever you open a file for Output, the file is completely overwritten. Add two buttons and try this code. Check the file after you click button1. The new file will have a long line full of numbers. Check the file after you click the second button. The line has only one letter. The rest is gone. Key is to open the file for "output."



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim fn As Integer
    fn = FreeFile()
    FileOpen(fn, "C:\Newfile.dat", OpenMode.Output)
    Print(fn, "123456789012345678901234567890")
    FileClose(fn)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim fn As Integer
    fn = FreeFile()
    FileOpen(fn, "C:\Newfile.dat", OpenMode.Output)
    Print(fn, "9")
    FileClose(fn)
    End Sub

  6. #6
    Join Date
    Jan 2003
    Posts
    165

    Re: deleting & writing in the same file

    Quote Originally Posted by Gizmo001
    In any case, you are wrong about strings being left over. Whenever you open a file for Output, the file is completely overwritten. Add two buttons and try this code. Check the file after you click button1. The new file will have a long line full of numbers. Check the file after you click the second button. The line has only one letter. The rest is gone. Key is to open the file for "output."



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim fn As Integer
    fn = FreeFile()
    FileOpen(fn, "C:\Newfile.dat", OpenMode.Output)
    Print(fn, "123456789012345678901234567890")
    FileClose(fn)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim fn As Integer
    fn = FreeFile()
    FileOpen(fn, "C:\Newfile.dat", OpenMode.Output)
    Print(fn, "9")
    FileClose(fn)
    End Sub
    i have to convince you,because maybe you don't understand my problem.
    rename that newfile.dat as newfile.ini to be able to see what have you written inside the file.
    after that ,before start playing with those 2 buttons,put 2-3 lines in that newfile.ini

    that's the ideea
    to rename some of the lines,not all
    so once that you ahve more lines,meanning that you press ENTER inside him(cr+lf)
    it will write the new strings over those specials characters,and it will be a mess,it will be erased all the content,actually overwritten.

  7. #7
    Join Date
    Dec 2002
    Posts
    305

    Re: deleting & writing in the same file

    I did what you told me to. I did not get any mess. Here is one line with all kinds of characters in a ".ini" file. I checked the files after running each button and could see all the characters as they were supposed to be. May be the editor you are using to read the file is the problem. For example, notepad does not show half the characters correctly. But Microsoft Word does. OR Maybe as you point out, I REALLY Don't Get it. If that is the case, I apologize for wasting your time.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If File.Exists("C:\Newfile.ini") Then
    File.Delete("C:\Newfile.ini")
    End If
    fn = FreeFile()
    FileOpen(fn, "C:\Newfile.ini", OpenMode.Output)
    Print(fn, "!@#$%^&*()!@#$%^&&**((!@#%^*(*&^%$#@#$%^&")
    FileClose(fn)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    fn = FreeFile()
    FileOpen(fn, "C:\Newfile.ini", OpenMode.Output)
    Print(fn, "^&((")
    FileClose(fn)
    End Sub

  8. #8
    Join Date
    Dec 2003
    Location
    St. Cugat - Catalunya
    Posts
    441

    Re: deleting & writing in the same file

    jasie24,
    as far as I can see you're trying something inconsistent with the basic concept of a sequential file.

    To "update" a sequential file you have to create a new one, but trying to overwrite it will never work correctly.

    You can "update" a binary file and even then you can not cross logical record boundaries. And of course you can open a text sequential file as a binary one, but you will have to deal with where cr+lf are, not to overwrite them, and so on, so it sounds really out of mind to me.

    Open your file, get a line at a time, write it (or the modified one) to a new file and, once done, delete the original file and rename the new one.

    Code:
    Dim InputFile as IO.StreamReader
    Dim OutputFile as IO.StreamWriter
    Dim TheLine as String
    
    FileCopy("MyIni.ini","BackupIni.ini")
    
    InputFile = New IO.StreamReader("BackupIni.ini", System.Text.Encoding.Default)
    OutputFile = New IO.StreamWriter("MyIni.ini", False)
    
    TheLine = InputFile.ReadLine
    While Not TheLine Is Nothing
        If ...your conditions here... Then
            OutputFile.WriteLine(TheLine)
        Else
            OutputFile.WriteLine("The replaced ine")
        End If
        TheLine = InputFile.ReadLine
    End While
    InputFile.Close
    OutputFile.Close
    
    'Kill "BackupIni.inI" if you don't want the backup anymore
    Hope it helps
    Last edited by DeepButi; October 28th, 2004 at 09:01 AM.
    Did it help? rate it.

    The best conversation I had was over forty million years ago ... and that was with a coffee machine.

  9. #9
    Join Date
    Dec 2002
    Posts
    305

    Re: deleting & writing in the same file

    Going back to your very first question, I created a file named newfile.ini that has the following 8 lines:

    ;comment
    [management=1]
    patid=34
    ;following fields not need it
    lastname=johnson
    firstname=tom

    [image]
    color=445


    I use the following declarations common to all procedures.

    Dim fn As Integer, a(8) As String
    Dim i As Integer

    I use the following button to read it:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    fn = FreeFile()
    FileOpen(fn, "C:\Newfile.ini", OpenMode.Input)
    For i = 0 To 8
    a(i) = LineInput(fn)
    Next
    FileClose(fn)
    End Sub

    And I use the following button to change and write the new text:

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    fn = FreeFile()
    FileOpen(fn, "C:\Newfile.ini", OpenMode.Output)
    For i = 0 To 8
    If i = 2 Then
    a(i) = "patid=2"
    ElseIf i = 4 Then
    a(i) = "LastName=Thomas"
    ElseIf i = 5 Then
    a(i) = "FirstName=Glen"
    End If
    Print(fn, a(i) & vbCrLf)
    Next
    FileClose(fn)
    TextBox1.ForeColor = Color.FromArgb(CInt(Mid(TextBox1.Text, 1, 3)), CInt(Mid(TextBox1.Text, 5, 3)), CInt(Mid(TextBox1.Text, 9, 3)))
    End Sub


    And I get the following result as you wanted:

    ;comment
    [management=1]
    patid=2
    ;following fields not need it
    LastName=Thomas
    FirstName=Glen

    [image]
    color=445


    Is this not what you wanted?

  10. #10
    Join Date
    Jan 2003
    Posts
    165

    Re: deleting & writing in the same file

    Now finally is working,this new variant is really working,,because,yes this was what i wanted,but it's different from all soutions until now,anyway so i thank you,for you solution and also for your persistance.

    thanks again.

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

Featured