Ronny Iversen wrote: > Hi :) > > Can anybody tell me whats the most efficient way of reading a file, > stripping a line from it, and writing the new result over > the old file. Is it possible to do this without having to read the > entire file into an array into memory first? Surprisingly, it the way to do this is to reduce the number of reads and writes to the absolute minium. If you need to search the entire file then read the whole file in one go. I/O operations to disk take much longer than writing to memory (millions of times longer!). So, if the file is not larger than a a couple hundred K your best bet is to do it in one read and one write. So, the questions are, how often do you have to do this and how big the file is. If you only do it once in a while then by all means just read the whole file into memory and then just write it back out. If you do it more then read the file into memory, remove all the lines you need to, and then write it back periodically or only when you have idle time. This would reduce it to less than one read and write per line removed. If the file is really large then keeping it in memory, or reading it all at once is a problem. The only way to optimize it would be to use a more structured format than a text file. The file would have to be divided into chunks that each can be loaded individually into memory. You see the hard drive has to load/save a whole sector no matter how little the data is. So it could take just as long to load a byte as to load a 64K file. If you have a small file load it all. If its larger restructure so that it can be divided into chunks that can be loaded all at once. > Please gimmme some oseudo code or something :) I can't give you any psuedo code unless I know how often you are doing this and how big the file is. > Regardz -- Phoenix -- President of The Artistic Intuition Company Caelius * Mirror Reflex * Runica * X-Domain * Infinite Realms http://www.io.com/~fenix +------------------------------------------------------------+ | Ensure that you have read the CircleMUD Mailing List FAQ: | | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html | +------------------------------------------------------------+
This archive was generated by hypermail 2b30 : 12/15/00 PST