CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #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.

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