• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

deleting from a file

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
AnswerTheMan
Asked:
AnswerTheMan
  • 8
  • 6
  • 4
1 Solution
 
Anthony PerkinsCommented:
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
 
aikimarkCommented:
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
 
Anthony PerkinsCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
aikimarkCommented:
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
 
AnswerTheManAuthor Commented:
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
 
aikimarkCommented:
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
 
AnswerTheManAuthor Commented:
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
 
Anthony PerkinsCommented:
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
 
aikimarkCommented:
I'll get back to with an example.
0
 
AnswerTheManAuthor Commented:
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
 
AnswerTheManAuthor Commented:
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
 
aikimarkCommented:
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
 
AnswerTheManAuthor Commented:
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
 
Anthony PerkinsCommented:
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
 
aikimarkCommented:
AnswerTheMan,

You could have increased the points of this question rather than opening a new question.  (for future problems).
0
 
aikimarkCommented:
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
 
AnswerTheManAuthor Commented:
acperkins : ok. sorry.
thank you both then.
0
 
aikimarkCommented:
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 8
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now