473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.
Feb 20 '08 #1
13 6934
Killer42
8,435 Expert 8TB
Simple - just use Get the same way you did the Put.
Feb 21 '08 #2
debasisdas
8,127 Expert 4TB
Try this sample code.

Expand|Select|Wrap|Line Numbers
  1. Dim v As String
  2. Open "E:\DEBASIS\DATA.DAT" For Binary As #1
  3. Get #1, , v
  4. Text2.Text = v
  5. Close #1
  6.  
Feb 21 '08 #3
Killer42
8,435 Expert 8TB
I'd recommend explicitly telling VB what you want, rather than relying on defaults. In other words...
Expand|Select|Wrap|Line Numbers
  1. Open "E:\DEBASIS\DATA.DAT" For Binary Access Read Shared As #1
  2.  
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...
Expand|Select|Wrap|Line Numbers
  1. Dim v As String
  2. Open "E:\DEBASIS\DATA.DAT" For Binary Access Read Shared As #1
  3. v = Space$(5)
  4. Get #1, , v ' Amount read depends on length of v.
  5. Text2.Text = v
  6. Close #1
  7.  
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.
Feb 21 '08 #4
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.

Expand|Select|Wrap|Line Numbers
  1.  Public Type CustomerRecord
  2.      CustomerID As String * 6
  3.      FullName As String * 20
  4. etc
  5.   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?
Feb 21 '08 #5
Killer42
8,435 Expert 8TB
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.
Feb 21 '08 #6
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!
Feb 21 '08 #7
Okay I'm having some trouble with this.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDeletCustomer_Click()
  2.   Dim FileName As String
  3.   Dim NewFileName As String
  4.   Open "c:\records.dat" For Random As #1 Len = Len(OneCustomer)
  5.   Open "c:\tempor.dat" For Random As #2 Len = Len(OneCustomer)
  6.  
  7.  
  8.  
  9. For loopindex = 1 To LOF(1) / Len(OneCustomer)
  10.      If loopindex <> (cboSelectCustomerD.ListIndex + 1) Then
  11.       Get #1, loopindex, OneCustomer
  12.       Put #2, loopindex, OneCustomer
  13.      Else: End If
  14. Next loopindex
  15.  
  16.   Close #1
  17.   Close #2
  18.  
  19. Kill "C:\records.dat"
  20.   FileName = "C:\tempor.dat"
  21. NewFileName = "C:\records.dat"
  22. Name FileName As NewFileName
  23.  
  24. Call UpdateCbo
  25. End Sub
cboSelectCustomerD 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
Feb 22 '08 #8
.........
Anyone? =<
Feb 24 '08 #9
Killer42
8,435 Expert 8TB
.........
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.
Feb 24 '08 #10
Killer42
8,435 Expert 8TB
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.
Feb 24 '08 #11
Killer42
8,435 Expert 8TB
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.
Feb 25 '08 #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.
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!!
Feb 25 '08 #13
Killer42
8,435 Expert 8TB
Hey, glad we could help. :)
Feb 25 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Moosebumps | last post by:
So say I am working on two separate .py files in IDLE that are part of the same program. Does anyone have problems where when you modify one file, and then run the other, the changes you made in...
4
by: Jerry | last post by:
I'm having just a bit of trouble wrapping my brain around the task of working with folders that are above the site's root folder. I let users upload photos (.jpg/.gif files) which can...
14
by: Mark B | last post by:
Our webhost (www.usbusinessweb.net) had a W2K IIS5 server crash after a scheduled hard-boot occurred during a ms-security patch install overnight. They couldn't get the server working again so they...
6
by: Matt Frame | last post by:
I have a client that has asked us to get a digital signature certificate and start digitally signing all files we pass between each other. I have heard of the subject and know about the certs but...
4
by: Axter | last post by:
Sorry for OT question, but does any one have a working *.bat file to get Comeau to work with VC++ 7.1, VC++ 8.0, or GNU 3.x compilers. I've tried everything I can think off, to get it to work,...
1
by: saturday | last post by:
I was having problems getting mysql to work with php on my apache server. Thankfully I got it working after reading a different thread, which had me copy over the libmysql file to the apache/bin...
0
by: George2 | last post by:
Hello everyone, From the definition of working set, it is a subset of virtual pages resident in physical memory -- from book Windows Internals. It means working set could not be larger than...
5
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have a machine running IIS 6.0. I just replaced the web.config and several aspx pages in the application and now the style sheets are not working. the images from the themes work but not the css...
3
by: Seth Williams | last post by:
I have done some new development on older files, locally - then I copy the files, along with the .vb files for the webservices, to our DEV server. Now, mysteriously, no web services are running - I...
6
by: josequinonesii | last post by:
I've searched, I've read, I've tested and re-read numerous post but to no avail yet... Quite simply, the settings I've applied to my httpd.conf, httpd-vhost.conf and my hosts files simply does not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.