in reply to Fastest way of changing the beginning of a txt file..

The simple answer is NO!

Think of a disk file like an old fashioned cassette tape. You start at the beginning of the tape and write stuff on the tape. You cannot go into the middle of the tape to delete stuff or add extra stuff - the tape recording has to be contiguous - a disk file is the same way. You can't replace a song in the middle of the tape with another song.

If you want to truncate a disk file (so the stuff at the beginning is not there anymore), the only way to do that is to "make a new cassette tape". Basically you read the file and process the data you want, then copy the rest of the original data to a brand new file. Then you rename that brand new file to the original file name. Whola, you have processed the data and truncated the original file.

In the cassette analog world, you would play cassette 1 until you skipped over the stuff you didn't want to save, and then start recording the output from cassette 1 onto a new cassette 2 from that point. The disk file system essentially works the same way.

THERE MAY BE ANOTHER WAY FOR YOU:

Unlike a cassette tape, a disk file can be "fast forwarded" very, very quickly.
As another analogy, think about a bookmark. When you read a long novel, you put a bookmark in so that you can restart reading again where you left off. You can do the same thing with a disk file. There is function, tell() where you can find out exactly what byte from the beginning of the file that you are currently at. There is a function, seek() where you can go back to that exact byte position where left off before.

So instead of actually truncating the file (which requires copying all the data that you haven't processed to another file), you can keep track of where you are in the input file (the exact byte offset). Going back to that exact byte can happen quickly, much faster than re-reading the first part of the novel. Of course this means that the actual storage space of the file on the disk doesn't get smaller.

You are asking about something that is possible, but unusual. Can you tell us more about what your application is?
Of course to seek() to where you left off, that info would have to be stored somewhere - maybe in another file or DB?

  • Comment on Re: Fastest way of changing the beginning of a txt file..