How To Rewrite The Corrected Value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kirubagari
    New Member
    • Jun 2007
    • 158

    How To Rewrite The Corrected Value

    [CODE=vb]Public Sub ReWrite_Open_Fi le(ByVal FileNo As Long)
    Dim FileSize As Long, Buffer() As Byte
    ' PART 1: Read the file.
    FileSize = LOF(FileNo) ' Determine how large the file is (in bytes).
    ReDim Buffer(1 To FileSize) ' Set our buffer to that length.
    Get #FileNo, 1, Buffer() ' Grab the entire file's data into the array
    ' PART 2
    ' If you plan to change the data, this is where it would happen.
    'Beep

    ' PART 3: Write the file back.
    If MsgBox("Write data back to the file?", vbYesNo) = vbYes Then
    Put #FileNo, 1, Buffer() ' Write the data back to the file.
    End If
    End Sub


    Private Sub Command2_Click( )
    Dim mHandle
    mHandle = FreeFile
    Open "a:\bonding.bin " For Binary Access Read Write Lock Write As mHandle
    ReWrite_Open_Fi le mHandle
    End Sub[/CODE]

    Why this function is not working? This function can execute but it can't overwrite after it changes the valus. I mean after it checked the value from byte 49 to mfile, if there is error it will check and correct the value. I want make it after it correct the value, it overwrite the file with the correct value.


    For eg:

    05 FF 45 65 78 65
    04 FF 45 65 78 65<-----CORRECTED (Want to overwrite the file after it changed the value to
    correct value)

    65 54 65 34 76 89
    04 FF 65 34 76 89<-----CORRECTED (same)


    I think you all can understand my problem
    Last edited by Killer42; Jul 23 '07, 01:18 AM. Reason: Added [CODE=vb] tag
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    This sub was written to demonstrate methods of reading and writing the data to/from the file in binary mode. As written, it doesn't actually achieve anything useful. It simply reads the entire file into an array, then immediately writes the array straight straight back to the file. The only visible result of this will be a new last-updated date/time on the file.

    I've just tested it (for the first time), and it does appear to successfully re-write the file in place.

    What you need to do is perform your correction to the array at the point labelled PART 2, so that the data written back to the file includes the corrections. In other words, the overall process would be to read the file into the array, correct the array, then write it back to the file.

    You can do this in a number of ways, including:
    • Insert your data-correction code into the ReWrite_Open_Fi le routine at "PART 2".
    • Place your data-correction code in a separate Sub and call it at the point labelled "PART 2".
    • (The preferred option) Incorporate the demonstrated file reading and writing techniques into your own code. As you can see, the actual file access is very simple.

    Comment

    • kirubagari
      New Member
      • Jun 2007
      • 158

      #3
      Killer42 i actualy cant understand what you are saying.But i can understand that the correction also translate into array whwn it write back data in to the file.Can u give me some examples so that i can understand and proceed with this problem

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        I don't know whether I can really explain it much more simply. The overall process is this...
        1. You open the file.
        2. You read the data from the file (into a byte array, in this case).
        3. You write the data back to the file (from the byte array).
        4. You close the file.
        Somewhere between steps 3 and 4 in this process, you need to make your corrections to the data held in the array. Otherwise, you are just putting back in the file the same information which was already there.

        The only meaningful change I could make in the routine as posted would be a call to some routine of yours which does the correction to the array, at the point labelled "PART 2". If you still can't get it to work, try playing around with it for a while. After all, including your correction to the array, you've really only got those five simple "blocks" to stick together. There are only so many ways you can logically do it.

        Comment

        • kirubagari
          New Member
          • Jun 2007
          • 158

          #5
          Public Sub ReWrite_Open_Fi le(ByVal FileNo As Long)
          Dim FileSize As Long, Buffer() As Byte

          Const a As Byte = 4
          Const b As Byte = &HFF
          Dim i As Long
          Dim AnyChanged As Boolean
          Dim changeMade As Boolean

          ' PART 1: Read the file.
          FileSize = LOF(FileNo) ' Determine how large the file is (in bytes).
          ReDim Buffer(1 To FileSize) ' Set our buffer to that length.
          Get #FileNo, 1, Buffer() ' Grab the entire file's data into the array






          ' PART 2
          ' If you plan to change the data, this is where it would happen.
          'Beep
          For i = 1 To 567 Step 6
          AnyChanged = False
          Debug.Print "Before : "; HexByte2Char(ar rByte(i)); " "; HexByte2Char(ar rByte(i + 1)); " "; HexByte2Char(ar rByte(i + 2)); _
          " "; HexByte2Char(ar rByte(i + 3)); " "; HexByte2Char(ar rByte(i + 4)); " "; HexByte2Char(ar rByte(i + 5))


          If arrByte(i) <> a Then
          arrByte(i) = a
          changeMade = True

          End If
          If arrByte(i + 1) <> b Then
          arrByte(i + 1) = b
          changeMade = True
          End If
          If changeMade Then
          AnyChanged = True
          Debug.Print "After : "; HexByte2Char(ar rByte(i)); " "; HexByte2Char(ar rByte(i + 1)); " "; HexByte2Char(ar rByte(i + 2)); _
          " "; HexByte2Char(ar rByte(i + 3)); " "; HexByte2Char(ar rByte(i + 4)); " "; HexByte2Char(ar rByte(i + 5))


          End If
          Next








          ' PART 3: Write the file back.
          If MsgBox("Write data back to the file?", vbYesNo) = vbYes Then
          Put #FileNo, 1, Buffer() ' Write the data back to the file.
          End If
          End Sub


          killer42 i can understand what you are trying to say.So i try put the byte correction function in the part2.So is it can work?But it doesnt work.So previously u ask me to put the byte correction in the part 2.I put the function over there.Am i have to change the coding or can used the same coding

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Two questions.
            1. What do you mean, exactly, by "doesn't work"? I can't tell what's going on if you don't give me details.
            2. I see your FOR loop goes from 1 to 567. I thought you were starting from 49?

            Comment

            • kirubagari
              New Member
              • Jun 2007
              • 158

              #7
              ya its starting from 49 to mfile size.I mistakenly type the ting.Just now i test the data from 1 byte until 597.I give the wrong coding to you.Sorry

              Actualy i put the code in the part2,but it does not work out.The thing cant overwrite.I do no weather im doing correct or not.....

              Comment

              • kirubagari
                New Member
                • Jun 2007
                • 158

                #8
                Doesnt work mean the things cant overwrite after it orrect the data from byte 49 to mfilesize.Why it cant overwrite

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by kirubagari
                  Doesnt work mean the things cant overwrite after it orrect the data from byte 49 to mfilesize.Why it cant overwrite
                  Can you explain in more detail what you mean when you say it can't overwrite? I know that the read/write code works, having tested it myself a couple of hours ago. So perhaps your floppy disk is write-protected or something?

                  Comment

                  • kirubagari
                    New Member
                    • Jun 2007
                    • 158

                    #10
                    This is some of the attachment that help you all understand my problem.The floppy is not write protected.When I change the data manually it can save the new byte.
                    Attached Files

                    Comment

                    • kirubagari
                      New Member
                      • Jun 2007
                      • 158

                      #11
                      killer 42 please help on this

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Originally posted by kirubagari
                        killer 42 please help on this
                        It's getting hard to do so, because this is split into so many separate threads. I did respond, in this thread, as did another expert.

                        I think you need to stick to one thread while you work through this thing, so we can all keep track of where we're going. I'm completely confused now about where things stand.

                        In debugging program code, you need to slow down and tackle one thing at a time. One of the most important concepts to keep in mind when debugging is to accurately identify and isolate the problem.

                        So, tell me this. If you just use the supplied code to read the file into the array and write it back, does the file come out exactly the same, but with the modified date/time updated? If so, then you know you can concentrate on the corrections being done within the array. If not, then we can ignore the array modification, because it makes no difference what we do to the array if we are not going to record it accurately.

                        Comment

                        Working...