Solved

deleting from a file

Posted on 2002-07-13
18
160 Views
Last Modified: 2010-05-02
i want to delete first 20 bytes from a file so the result will be the same file starting from the 21st byte
of its initial state.
of course i know how to do it by reading from the file what i want and writing it to a FRESH NEW FILE. BUT - try to do it with 5MB file and it will take you too much time.
i want to do it on the file itself.
0
Comment
Question by:AnswerTheMan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 4
18 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7151598
You have to re-write the file.  There are ways to optimize that, but there is no way around it.  Let us know if you need help with that.

Also, please maintain your open questions:
Enterprise Editio  VS  Enterprise Edition  :))))) Date: 05/29/2001 09:25AM PST  
http://www.experts-exchange.com/visualbasic/Q_20126260.html
Print job Date: 03/16/2001 12:23PM PST
http://www.experts-exchange.com/visualbasic/Q_20092846.html
300 points in VB - a verbal alogritem needed Date: 03/15/2001 03:22PM PST  
http://www.experts-exchange.com/delphi/Q_20092388.html
(you should delete this last question)

Anthony
0
 
LVL 45

Expert Comment

by:aikimark
ID: 7152193
Dim intFilenum As Integer
Dim strContents As String
intFilenum = FreeFile
Open "path and filename" For Input As #intFilenum
strContents = Input(LOF(intFilenum), #intFilenum)
Close (#intFilenum)
intFilenum = FreeFile
Open "path and filename" For Output As #intFilenum
Print $intFilenum, Mid$(strContents , 21)
Close (#intFilenum)
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7152276
aikimark's solution confirms that you must re-write the file and something like he/she suggests should work for you.  However, since your files are large, you may not get very good performance from reading the whole file (6.5MB) into a string variable and then writing the whole string at once.  Instead, it may be more efficient to use 16Kb or 64Kb blocks.

Anthony
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 45

Expert Comment

by:aikimark
ID: 7152312
You might also be able to open the file in Binary mode and loop through the file, assigning characters to the position of 21 bytes earlier in the file.

If you do this:
1. You will either copy individual bytes or chunks of data.  
2. You will need to truncate the file (i.e. set the file size length down by 21 bytes)

Binary example:
http://216.26.168.92/files/openstatement/index5.html
0
 
LVL 5

Author Comment

by:AnswerTheMan
ID: 7159884
aikimark 1st code is almost the same as the code i use. the only diffrent is that in writing i open for Binary and use PUT instead of print.
i've just tried to put bookmarks and took time for every action doing it on a 3.8MB mp3 file.
the reading of the original file into a STRING took about 30 seconds. the writing to the new file took less then 5 seconds.
now, i've compared that to running winzip on the same file. since winzip does not compress mp3 - it took it 5 seconds to zip the file, and it's writing a new one as well. so winzip writes a new file like that - 7 times faster then my code.
since most of the time is wasted on reading the file into the string - do you think that by reading smaller groups - i can make it 7 times faster, or there is something more basic in the whole approach ??
----------------------------------------
aikimark - i don't understand your second comment.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 7159923
In my second comment, I am only opening one file and moving the contents -21 bytes.

=======================================
An alternative to the first example might be to use byte datatype variable instead of a string datatype.
0
 
LVL 5

Author Comment

by:AnswerTheMan
ID: 7162634
well i've tried to read diffrent sizes of blocks while looping the file instead of the whole file into 1 string.
the best result is the same as the previous siyuation, so that's get me nowhere.
as for your second idea : "moving the contents -21 bytes" is exactly what i'm after, but those 20 bytes should not be in the file anymore. i mean - the file must be samller.
if that's what you are saying - can you deliver some code here for that ?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7162764
Let me repeat my original comment:

>>You have to re-write the file.  There are ways to optimize that, but there is no way around it. <<

Anthony
0
 
LVL 45

Expert Comment

by:aikimark
ID: 7162968
I'll get back to with an example.
0
 
LVL 5

Author Comment

by:AnswerTheMan
ID: 7164095
acperkins : read my last comment and you'll see that using diffrent sizes of blocks did not optimized it. for example - timing the process with 64KB blocks almost doubled the time. so ok, maybe "but there is no way around it" , but do you know other way(s)  to "There are ways to optimize that"  ???
this 3.8MB mp3 file is my test file. if winzip rewrites it in 5 seconds on this machine - i'll settle for 10 seconds for my code, but not 30 seconds.
0
 
LVL 5

Author Comment

by:AnswerTheMan
ID: 7164805
acperkins, aikimark :
in few minutes i'll post a new question ("deleting 20 bytes from a file") instaed of this one. you are invited to make your comments there. thanks.
0
 
LVL 45

Accepted Solution

by:
aikimark earned 200 total points
ID: 7166596
why open a new question on the same topic?

the answer I would have given you has already been supplied in your new thread.

Please close this question.
0
 
LVL 5

Author Comment

by:AnswerTheMan
ID: 7166732
aikimark : i belive you. you were going to come with the answer here. i konw. but since i wanted best optimization i found it better to draw people attention with 500 points new question.
since i belive you would have come with an answer - i'm giving you this question points.
tell you the true - i asked to delete this question but
it seems that acperkins here is well connected with community support and they claim he's intiteled to get the points here.....would you belive it ???
exept of bla bla bla he did not come with any code. and they want me to give him the points for bla bla bla.
even before i decided to put a new question i found YOUR comments much more usefull then his.
thank you
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7166797
AnswerTheMan,

>>acperkins here is well connected with community support and they claim he's intiteled to get the points<<

I am very flattered, but in this one case I have to plead innocent and as far as I am concerned you are absolutely right.

I am glad you got it working,

Anthony  
0
 
LVL 45

Expert Comment

by:aikimark
ID: 7166824
AnswerTheMan,

You could have increased the points of this question rather than opening a new question.  (for future problems).
0
 
LVL 45

Expert Comment

by:aikimark
ID: 7166828
AnswerTheMan ,

Also understand that your solution's performance degrades as hard drive fragmentation increases, due to head movement and latency.  You might want to run some (proposed solution) tests under less-than-ideal circumstances if you can (re)create a large fragmented file.

I'm going to add my name to the new question, since I want to see the performance figures.
0
 
LVL 5

Author Comment

by:AnswerTheMan
ID: 7171445
acperkins : ok. sorry.
thank you both then.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 7171563
answertheman,

I think you'll find my binary example link is very similar to the MrRoper solution, except for the chunk size.

I've been looking for a solution using the memory-mapped file API, since your chosen solution is quite fast.
http://www.experts-exchange.com/visualbasic/Q_20325478.html

I'm glad you've got your answer.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question