-
Sep 5th, 2007, 07:17 AM
#1
Thread Starter
Hyperactive Member
Editing a file
I want to write a VB program to remove a specified number of bytes from a file. For example I have a file 8 MB in size. I want to remove the first n bytes from this file.
Of course I can do a byte by byte copy from the n+1th to the last byte to a new file, then filecopy the new file to the original file, but that will be less time efficient-shows a noticeable delay when the file is huge. Is there a more time efficient method of doing this.
Any help is welcome.
-
Sep 5th, 2007, 09:03 AM
#2
Re: Editing a file
I posted about a similar problem some time ago, had to open a ca. 7 Mb file, do some decoding operations and then plot to a bitmap. The solution was to write c code and place it in a dll to be called from vb. That really sped things up.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Sep 5th, 2007, 09:22 AM
#3
Thread Starter
Hyperactive Member
Re: Editing a file
dear krtxmrtz
I dont know C or C++.
Isn't there some method to do it using VB only?
-
Sep 5th, 2007, 10:07 AM
#4
Re: Editing a file
Originally Posted by svg3414
dear krtxmrtz
I dont know C or C++.
Isn't there some method to do it using VB only?
Well, I don't usually work with large files -other than those I've mentioned, so I'm not sure if there are any VB input/output statements or any tricks than can make a difference in terms of time. About the C thing, since opening a file, clipping a few bytes and storing to a new file it is a relatively simple operation, maybe you can post to the c forum and ask for code. (I used to know C but have all but forgotten). Then you can wrap it up into a dll if you have VC++ in your Visual Studio.
In this thread
http://www.vbforums.com/showthread.p...&highlight=dll
you have a template in post #29.
Last edited by krtxmrtz; Sep 5th, 2007 at 10:23 AM.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Sep 5th, 2007, 10:20 AM
#5
Re: Editing a file
Either way if it's VB or C, you still have to re-write the entire file, except that C/C++ is a little faster than VB...
-
Sep 5th, 2007, 10:26 AM
#6
Re: Editing a file
Originally Posted by CVMichael
Either way if it's VB or C, you still have to re-write the entire file, except that C/C++ is a little faster than VB...
According to someone who posted in the thread I've provided the link for, c can be 10 to 100 times faster. I wonder if that referred just to processing a large amount of bytes already in memory. Maybe it's less efficient when it comes to HD input/output?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Sep 5th, 2007, 10:59 AM
#7
Re: Editing a file
He refered to string parsing...
But if you know how to write code in VB optimized for speed, you can get to almost the same speed as C. Just look at the code that Merri writes... it's unbelivebly fast.
If you do this in C, you would have to work with Char arrays (Byte arrays), therefore, if you do this in VB and you use Byte arrays, and since you run the C code compiled, so should you run the VB code compiled, and if you add optimizations (like removing "Array bounds checks" and the other options from Advanced Optimizations), same as C, it does not do any of these checks, you will most likely get about the same speed as you would get with C.
Merri once said, that the VB compiler is a hacked version of a C compiler.
In other words, it is possible to get the same speed with VB as you get in C if you know how to write that code so that the compiler will interpret it the same way it would interpret the C code.
Anyways... back to the subject...
Theoretically, there is another way to "remove" data from a file without re-writing the entire file.
A file is written on the disk in sectors, depending on how you format the hard-drive, the sector size is different.
And also each sector could be in different parts of the hard-drive, this happens when the hard-drive is fragmented. (that's why we run defrag once in a while)
Again... In theory, you could "free" the sector that belongs to the file where the data that you need to delete is, without deleting the whole file. But the problem is that if you want to delete 10 bytes, and you delete the sector, then you may delete 512 bytes (that how much a sector size usually is). So in that case, you have to write in another sector only the data that you need, delete the old sector then link the new sector to the file. Kind of like how you work with the entire file, but you do this at sector level.
I have never actually done this, and I'm pretty sure it's possible, but I doubt that it can be done with VB.
-
Sep 5th, 2007, 11:02 AM
#8
Re: Editing a file
By the way... I have code here to delete parts of a file, but it re-writes the entire file, so it's slow...
VB6 - Insert & Delete data from a file
-
Sep 5th, 2007, 11:48 AM
#9
Re: Editing a file
Originally Posted by CVMichael
He refered to string parsing...
But if you know how to write code in VB optimized for speed, you can get to almost the same speed as C. Just look at the code that Merri writes... it's unbelivebly fast.
...
OK, so, you can parse strings as fast as C does if you know how to write code in VB optimized for speed. Now, how about reading from / writing to HD, no real advantage on the C side here?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Sep 5th, 2007, 12:15 PM
#10
Re: Editing a file
A little bit, but I don't think it's significative
-
Sep 5th, 2007, 01:37 PM
#11
Re: Editing a file
Originally Posted by CVMichael
Theoretically, there is another way to "remove" data from a file without re-writing the entire file.
A file is written on the disk in sectors, depending on how you format the hard-drive, the sector size is different.
And also each sector could be in different parts of the hard-drive, this happens when the hard-drive is fragmented. (that's why we run defrag once in a while)
Again... In theory, you could "free" the sector that belongs to the file
Depending on the file system. In a FAT file system you'd have to work on whole clusters.
I have never actually done this, and I'm pretty sure it's possible, but I doubt that it can be done with VB.
I used to do things like that in DOS (I used to partition drives using a raw disk editor too ) but I don't think you can link and unlink sectors or clusters using VB (unless there are APIs for it).
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Sep 6th, 2007, 10:57 AM
#12
Thread Starter
Hyperactive Member
Re: Editing a file
That brings us to the question
In VB is there a way to access a file sector by sector ?
-
Sep 6th, 2007, 12:23 PM
#13
Re: Editing a file
I don't think so...
But either way.... I brought up this idea, but I don't think you should follow up on it because, first of all, it's very complicated, and second, you could screw up your hard-drive big time if you make any mistakes...
A sector could contain more than one file (or parts of multiple files), and you will have to edit only the file in question leaving the others intact...
Just imagine making changes to a sector where you have your file + maybe parts of kernel.dll or registry....
Or am I confuzing this with clusters ? hmmm I have to re-fresh my memory...
Anyways... be carefull with this...
-
Sep 7th, 2007, 11:12 AM
#14
Thread Starter
Hyperactive Member
Re: Editing a file
Originally Posted by CVMichael
I don't think so...
A sector could contain more than one file (or parts of multiple files), and you will have to edit only the file in question leaving the others intact...
No, that is not correct. A sector will have only part of a single file.
-
Sep 7th, 2007, 11:14 AM
#15
Re: Editing a file
Originally Posted by svg3414
That brings us to the question
In VB is there a way to access a file sector by sector ?
Again, for something like this you are better off with C/C++
-
Sep 8th, 2007, 12:09 AM
#16
Thread Starter
Hyperactive Member
Re: Editing a file
Originally Posted by Hack
Again, for something like this you are better off with C/C++
Ok now I will have to learn C.
Let us treat this topic as closed.
-
Sep 8th, 2007, 03:37 AM
#17
Re: Editing a file
Originally Posted by svg3414
Ok now I will have to learn C.
Let us treat this topic as closed.
Well, if that's all you need c for and you think you can't afford the learning time and effort, maybe you can ask for help in the c forum, the code may be simple and someone may be willing to provide it.
Last edited by krtxmrtz; Sep 8th, 2007 at 03:57 AM.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Sep 8th, 2007, 10:45 AM
#18
Thread Starter
Hyperactive Member
Re: Editing a file
Originally Posted by krtxmrtz
Well, if that's all you need c for and you think you can't afford the learning time and effort, maybe you can ask for help in the c forum, the code may be simple and someone may be willing to provide it.
OK , Thanx, I will try it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|