Question

Deleting random access records and put byte data type in record

Asked by: damirsel

Hello.
 I have two questions:

1. How to delete record from file opened with Random access?

2. How to put a binary data (*.bmp, *.gif, *.exe)  in random access opened file and to be usefull after reading again. I tried but it only works when the file is opened in binary mode but then I can only get one (first) record written in file. Is there a way to make file in binary mode to work like file in random mode when reading records?

Thanks

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-12-07 at 10:17:29ID21233675
Tags

access

,

random

,

read

,

records

Topic

Visual Basic Programming

Participating Experts
3
Points
50
Comments
14

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Random and Binary mode
    What is the essencial diference between Random mode and Bynary mode when i create a file.? When is the best to use one or the other? Regards. Francisco
  2. Random
    I want to be able to use a javascript to display a random image out of a set of 4 on my web page. Anyone know the code for this?
  3. BMP to GIF
    Looking for source code in Visual Basic 5.0 or 6.0 to convert a bmp to gif. It can be a control or dll as well and not just source code. Thanks, Jeff
  4. randomize >>
    hi guys >> i am using this code to generate a code <% dim rpic rpic = Request.QueryString("pic") if rpic = "" then Randomize rpic = Int((100 * Rnd) + 1) end if %> <% rpicn = rpic +1 rpicp = rpic - 1 %> then i am calling <img src=&qu...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: osmodeanPosted on 2004-12-07 at 10:36:22ID: 12766801

Deleting Records

You could delete a record by clearing its fields, but the record would still exist in the file. Usually you dont want empty records in your file, because they waste space and interfere with sequential operations. It is better to copy the remaining records to a new file, and then delete the old file.

To remove a deleted record in a random-access file

   1. Create a new file.

   2. Copy all the valid records from the original file into the new file.

   3. Close the original file and use the Kill statement to delete it.

   4. Use the Name statement to rename the new file with the name of the original file.


Source: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconusingrandomfileaccess.asp

 

by: osmodeanPosted on 2004-12-07 at 10:38:52ID: 12766821

I do not know exactly what you mean with your second question, but the info I found here might proof useful:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconusingrandomfileaccess.asp

 

by: damirselPosted on 2004-12-07 at 10:52:45ID: 12766949


In second question I will like to know is there a way to for example do next:

1. open some (bmp, jpg, exe...) file for binary access read as #1 and get its contents
2. open second file for random as #1 and put content of first file in second like standard record wich can be later used properly?

OR IF THIS IS NOT POSSIBLE THEN:

Is there a way to manage records in binary file mode like in random?

 

by: Idle_MindPosted on 2004-12-07 at 11:33:34ID: 12767327

The problem with your second question is that image files are not the same length, therefore you cannot access them via a recordnumber because you don't know where one record ends and the next begins as in a typical random access file.

The only way to do this would be to create your own custom header to store the byte position and length of each image in the file just as a Zip file would.

~IM

 

by: EnladePosted on 2004-12-07 at 14:52:21ID: 12769132


To do what you want to do with Binary information of variable size you will need to set up some large fixed Byte array to store each set of Binary information into and also store the size.  That is not a good way to do it.

It would be better if you just opened the file as Binary and stored a seek point at the beginning of each chunk of binary data (or keep the seek points in a seperate file).  Maybe store the seek point of the Previous/Next record.  For instance, create a record like this.

Public Type BLOBHeaderType
  BLOBValid As Boolean
  BLOBSeek As Long
  BLOBSize As Long
  PrevSeek As Long
  NextSeek As Long
End Type

Rather then Delete anything you can simply mark it as Invalid by BLOBValid = False.  Programitically ignore any chunk of data that has BLOBValid set to False.  If you must conserve disk space you can write a function that compacts the file getting rid of any data that has BLOBValid set to False, but that doesn't need to be done on the fly.  Only when things get too large.

Technically, you don't need a BLOBSeek point since you know that it must start a right after the record.  So you could just get the record size and advance you file pointer.  But I just threw it in if you would rather use it.  You also don't need a BLOBSize since the BLOB size is the NextSeek minus the BLOBSeek point.  But again, you might as well just store that information rather then compute it.

Now, all you do is the following.

Open the file for Binary and get the file pointer to the end of the file
You do this by walking through the file reading the headers and jumping to the NextSeek point.
It is important that you remember the last seek point so you can put it into PrevSeek of the new record.
Save the last seek point into PrevSeek after you reach the end of the file.
Save the current seek point into another variable called CurrHeaderSeek
You will need that so that you can come back and rewrite this record after you fill it up with real data.
But for now Write a dummy BLOBHeader to the file (just to make room for it).
Save the new current seek point into BLOBSeek (in your header record)
Save the size of the binary data in your BLOBSize (in your header record)
Write the Binary data to the file
Save the new current seek point into NextSeek (in your header record)
Set the BLOBValid to True (in your header record)
Seek back to the dummy BLOBHeader (that is seek back to CurrHeaderSeek)
Resave the proper BLOBHeader that we filled up nicely.

Now, what you probably want to do is add more information about each BLOB to the BLOBHeader record.  Like if this is JPG's then maybe the JPG name or some other such information that might be useful to the user.

Still, in general the above is probably the way to go.  No, actually the way to go would be to toss your BLOBs into a DB (like Access or Personal SQL Server or whatever).

 

by: EnladePosted on 2004-12-07 at 14:57:19ID: 12769163


You know you could also jump to the end of the file directly.  However,  you still need to know the Previous seek point.  So, you could always save a dummy record at the end of the file whose data was all junk except for the PrevSeek value.  That way, you could just jump to the end of the file and then move back the size of a record and read just that dummy record (that already has the PrevSeek value set properly).  That is probably the way I would do it since I don't like the idea of walking through the entire file (particularly if it is large).  Just more for you to consider.

 

by: EnladePosted on 2004-12-07 at 15:01:27ID: 12769184


In other words, add this last step to the above.

Save CurrHeaderSeek into a dummy BLOBHeader and save it to the end of the file.

And instead of reading through the entire file you would do the following.

Open the file for Binary.
Move to the end of the file minus the BLOBHeader size.
Read in the working BLOBHeader that contains the PrevSeek value already set.

Mind you you will have to handle the special case when the file is first created (empty file).  But that shouldn't be a problem.

 

by: EnladePosted on 2004-12-07 at 15:03:06ID: 12769196


I know that all sounds confusing, but it is really easier then what I am making it out to be.  Its just harder to explain it then to actual do it.  Maybe I'll try to explain it better if you don't understand.  Or maybe someone else wants to write you a little sample code along those lines.

 

by: Idle_MindPosted on 2004-12-07 at 15:39:15ID: 12769383

It's a clever system and definitely doable.

I don't think you need BLOBSeek, or BLOBSize for that matter, since after you read that structure, the file pointer will already be sitting in the BLOB data waiting for the next read.  To read the structure, you would already have it defined, and consequently know the size.

~IM

 

by: EnladePosted on 2004-12-07 at 19:44:25ID: 12770470


By BLOB I meant the Binary portion that followed the structure.  You will need to know that in order to indicate the number of bytes to read.  But you are correct that you can figure out the number of bytes based on the value of NextSeek (with a bit of fudging).  Basicly the file will look something like this (assuming three JPG's or whatever)

BLOBHeader1
Binary Data1 of Arbitrary Size
BLOBHeader2
Binary Data2 of Arbitrary Size
BLOBHeader3
Binary Data3 of Arbitrary Size
BLOBHeader4

Were,

BLOBHeader1.BLOBValid = True
BLOBHeader1.BLOBSeek = Seek of Binary Data1 (as mentioned this can be calculated)
BLOBHeader1.BLOBSize = Byte count for Binary Data1 (as well, this can be calculated)
BLOBHeader1.PrevSeek = -1
BLOBHeader1.NextSeek = Seek of BLOBHeader2

BLOBHeader2.BLOBValid = True
BLOBHeader2.BLOBSeek = Seek of Binary Data2 (as mentioned this can be calculated)
BLOBHeader2.BLOBSize = Byte count for Binary Data2 (as well, this can be calculated)
BLOBHeader2.PrevSeek = Seek of BLOBHeader1
BLOBHeader2.NextSeek = Seek of BLOBHeader3

BLOBHeader3.BLOBValid = True
BLOBHeader3.BLOBSeek = Seek of Binary Data3 (as mentioned this can be calculated)
BLOBHeader3.BLOBSize = Byte count for Binary Data3 (as well, this can be calculated)
BLOBHeader3.PrevSeek = Seek of BLOBHeader2
BLOBHeader3.NextSeek = Seek of BLOBHeader4

And of course, to make this more efficient you would store a trailing dummy BLOBHeader like this:

BLOBHeader4.BLOBValid = False
BLOBHeader4.BLOBSeek = -1
BLOBHeader4.BLOBSize = -1
BLOBHeader4.PrevSeek = Seek of BLOBHeader3
BLOBHeader4.NextSeek = -1

Now to delete a BLOB you would simply mark its BLOBValid value as False.  Then when you read through the file you would ignore any BLOBs that had BLOBValid of False.

Then to compress the file and eliminate any False valued BLOBValid BLOBs you could do what osmodean recommended (copy each BLOBValid True BLOB to a seperate file ignoring any False BLOBs and then adjust the Seek values in each BLOBHeader).

Now, if you are up for a more of a challange there are more interesting ways of eliminating the False BLOBs from your file.  This is to say that you could consolidate the True BLOBs to the begining of the file and then keep track of the extra space created by the void at the end of the file (overwritting it with new True BLOBs).  As well, this is not so bad since you can move BLOBs as a chunk (since you know the entire size of the BLOBHeader and BLOB Binary data).  This will be much faster then simply moving one byte at a time (by which I mean, having VB read and write one byte at a time).  But that is another lesson (interesting as it is).

 

by: EnladePosted on 2004-12-07 at 19:51:31ID: 12770503


Oh, I think that there might be a few minor pitfalls to using VB records that I should mention.  Depending on the types of data that you are storing in the record you might end up getting a bit of padding at the end of each record.  This is to say that you might end up with a byte or two extra at the end of each record (in the file itself).  That is important if you want to know the actual size of the record in the file (not in memory).  I mention this without thinking about it in any way.  It might not be a problem, it might be a problem, if I thought more about it I could tell you.  I only mention it just in case you toy with this idea and find that you are having trouble locating the next record or locating the Binary data (depending on how you get the Seek values, that is, whether you ask VB for it or whether you calculate it based on the memory sizes of the records (which is sometimes different then the actual size on disk)).  Just a thought....that I didn't have....cause I didn't think about....what you might want to think about....if you encounter such a problem..... :)

 

by: EnladePosted on 2004-12-07 at 20:06:35ID: 12770559


Idle_Mind:  I think that what I said above is pretty much what you were suggesting in your message.  Wish I had read your message so that it could have saved me time on typing....hee hee.

Actually, I read the question and then left my desk for a bit then came back and answered it without doing a refresh.  That'll learn me.

 

by: Idle_MindPosted on 2004-12-09 at 07:40:43ID: 12784276

Hi damirsel,

Is Enlades proposal something you want to pursue?  I don't think anyone will move forth with coding it without a nod from you since it is conceptually easy buy not necessarily trivial to code.

~IM

 

by: damirselPosted on 2004-12-12 at 06:11:03ID: 12803829

I was busy for a few days so I did not check my mail, anyway basically I get the point
Thank you Enlade and Idle_Mind.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...