Working with .dat files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IraqiAli
    New Member
    • Feb 2007
    • 19

    Working with .dat files

    Okay, I'm totally new to this. The only thing I know is how to store something in a dat file by opening a file, using put then closing it.

    I'm storing multiple records in a file. Each record has about 6 fields. I also want to be able to display idividual fields in a particular record into individual textboxes, how do I go about doing this?

    eg Customer ID, Customer Name, Address, Phone Number etc stored in a dat file, using put #filenumber, , customerID etc.
    I now want to display this information in textboxes, txtCustomerID, txtCustomerName etc.

    Thank you.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Simple - just use Get the same way you did the Put.

    Comment

    • debasisdas
      Recognized Expert Expert
      • Dec 2006
      • 8119

      #3
      Try this sample code.

      [code=vb]
      Dim v As String
      Open "E:\DEBASIS\DAT A.DAT" For Binary As #1
      Get #1, , v
      Text2.Text = v
      Close #1
      [/code]

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        I'd recommend explicitly telling VB what you want, rather than relying on defaults. In other words...
        [CODE=vb]
        Open "E:\DEBASIS\DAT A.DAT" For Binary Access Read Shared As #1
        [/CODE]

        Hm... you know, I think we may have a problem here. I'm pretty sure that you have to indicate how many bytes to read when you use Get. In other words, if you wanted to read 5 characters you would do something like this...
        [CODE=vb]
        Dim v As String
        Open "E:\DEBASIS\DAT A.DAT" For Binary Access Read Shared As #1
        v = Space$(5)
        Get #1, , v ' Amount read depends on length of v.
        Text2.Text = v
        Close #1
        [/CODE]For many data types such as Integer, Long etc, this wouldn't matter because they are a specific size. But with a string, I don't know what you'll do. You might need to store the length first. Or instead of using binary format, use Print # or Write # to write in text format, then use Input # or Line Input # to read it back after opening for Input rather than Binary.

        Comment

        • IraqiAli
          New Member
          • Feb 2007
          • 19

          #5
          Okay I managed to display each individual field, but I had to first declare each field type with it's size, in a seperate module.

          Code:
           Public Type CustomerRecord
               CustomerID As String * 6
               FullName As String * 20
          etc
            End Type
          I now have no problem's with it, thank you ^_^

          ..but I know have another question. Whats the code that lets me delete a certain record from a dat file?

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Ah, that's not as simple a question as you might think. Changing the data in a file is perfectly simple in Binary mode, as long as the length of the data doesn't change.

            To remove part of a file, you basically need to rewrite everything from that point on, and truncate (shorten) the file. Though there are ways to do that, it's basically risky because if anything goes wrong part-way through, you could totally ruin the file. Plus, I'm not sure how you go about truncating the file (though I'm fairly sure it's possible).

            The more commonly-used technique is to write a copy of the file without the unwanted piece, then delete the original file and rename the new one. This is how many archiving utilities work, for example (Eg. WinZIP).

            Of course, it's also possible to "logically delete" a record. That is, make some change to it so that it is ignored in future. Thus, while physically it is still there, you can consider it as having been deleted. This is actually how a lot of database programs "delete" records, which is why the database keeps getting larger and has to be "compacted" from time to time to remove the old junk.

            If you try playing with the search box up top, you'll find this topic has been covered at least once or twice.

            Comment

            • IraqiAli
              New Member
              • Feb 2007
              • 19

              #7
              Alright! Thank you very much for your time, you've really helped me out a lot.
              I did know about logically deleting a record, but I didn't want the files to keep increasing in size over a long period of time.

              I think I'll take a look at how to copy to a new file and rename.

              Thanks again!

              Comment

              • IraqiAli
                New Member
                • Feb 2007
                • 19

                #8
                Okay I'm having some trouble with this.

                Code:
                Private Sub cmdDeletCustomer_Click()
                  Dim FileName As String
                  Dim NewFileName As String
                  Open "c:\records.dat" For Random As #1 Len = Len(OneCustomer)
                  Open "c:\tempor.dat" For Random As #2 Len = Len(OneCustomer)
                
                
                
                For loopindex = 1 To LOF(1) / Len(OneCustomer)
                     If loopindex <> (cboSelectCustomerD.ListIndex + 1) Then
                      Get #1, loopindex, OneCustomer
                      Put #2, loopindex, OneCustomer
                     Else: End If
                Next loopindex
                
                  Close #1
                  Close #2
                
                Kill "C:\records.dat"
                  FileName = "C:\tempor.dat"
                NewFileName = "C:\records.dat"
                Name FileName As NewFileName
                
                Call UpdateCbo
                End Sub
                cboSelectCustom erD basically has a list of all the customer names, so I can select a name from the list and choose to delete it.
                The code works perfectly when I choose to delete the last record in the file.
                The problem arises when I select a record other than the last. It copies all the records I need to the new file, but there's also a gap where the old unwanted record used to be. It's because it's writing to the record number "loopindex" . I want the code to write over the gap after it has missed out the record I don't want, I tried several different approaches but I still couldnt get it to work.

                Please help =s

                Comment

                • IraqiAli
                  New Member
                  • Feb 2007
                  • 19

                  #9
                  .........
                  Anyone? =<

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by IraqiAli
                    .........
                    Anyone? =<
                    Sorry, it can take a while before anyone has time to look at questions here. We're just volunteering our own time, after all.

                    It's Monday morning here. I'll try to have a look at this at lunchtime.

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      I did have a quick glance at the code, and saw what looks like a logic bug. I think in the FOR loop, you need to do the Get every time 'round, and only the Put should be dependent on the If test.

                      Also, I think (not positive about this) that you should leave out the record number parameter in the Get and Put. In other words, Put #2, , OneCustomer. I've used binary mode a fair bit, but not random mode. So I'm not certain this is the right way to go about it.

                      See whether this makes any sense, and I'll try to get back to it at lunchtime.

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        I finally had a chance to read what you actually said about the code, and it seems I was correct. The Get/Put logic is where the problem lies. You need to read all records, but only write out the wanted ones.

                        Leaving out the record number on the Put should do the trick, if this syntax is valid for random mode. I know it is for binary, but still not sure about random.

                        Otherwise, just use two counters, one for read position and one for write position. They will both be incremented by one each time around the loop, except that the write counter won't increment when you skip a record.

                        If performance becomes an issue (in other words, if it's too slow), then let me know. I believe we can make this process much faster with a fairly simple fix, using binary mode instead of random.

                        Comment

                        • IraqiAli
                          New Member
                          • Feb 2007
                          • 19

                          #13
                          Originally posted by Killer42
                          I finally had a chance to read what you actually said about the code, and it seems I was correct. The Get/Put logic is where the problem lies. You need to read all records, but only write out the wanted ones.

                          Leaving out the record number on the Put should do the trick, if this syntax is valid for random mode. I know it is for binary, but still not sure about random.

                          Otherwise, just use two counters, one for read position and one for write position. They will both be incremented by one each time around the loop, except that the write counter won't increment when you skip a record.

                          If performance becomes an issue (in other words, if it's too slow), then let me know. I believe we can make this process much faster with a fairly simple fix, using binary mode instead of random.
                          I know you're using your own time to help others, i'm sorry for bumping the thread like that! It's just I don't have that much time to complete this, I really want to get it done.

                          I don't think performance would be much of an issue, I'm not going to have that much data stored on my file anyway.
                          You have solved the problem for me, I really can't thank you enough!! You should really have a rep system for this forum :)

                          Thanks again!!

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            Hey, glad we could help. :)

                            Comment

                            Working...