Quote:
Originally Posted by Keiro
Heh, I got it working now. ^^'' Had to find a script which read the while of a file then I wrote it back with the new stuff at the beginning and added a few \n's.
What I ment by not wanting to load into main memory is cos I don't want to load large files like a few MB then rewrite them. I know its not much but still. =P
|
How does your approach solve this problem? You cannot add to the beginning of a file directly. If you wish to add to the beginning, you must do one of two things:
Method one: Read the entire file into memory, and modify the contents there. Write out the modified file when done. Slight variation on this is to use a memory-mapped file (useful if the file's size exceeds physical memory available).
Method two: Create a new file with your new line. Append the old file's contents to it. Delete the old file, and rename the new one to match the old one.
This brings me back to my point about your reasons for doing this. For example, if you're treating each line in the file as an element, and wish the file to act as a stack of elements with the beginning of the file being the top of the stack, why not just alter your code so the end of the file is the top of stack? This is just an example; since you won't tell us why you're doing this, we can't be of any more help than this.