![]() |
Read file backwards
Hey,
I'm writing a script that appends to a file. Would it be possible if someone could tell me how to read a file from down up rather form up down. Example: the dog sat I want this to happen sat dog the So the file is read from down up line by line. Many thanks in advance, K |
You can't read a file backwards. You can move near the end of a file and read the last part forwards, then you can move back a bit and read another part forwards, then you can reverse each of the parts and pretend that you read it backwards, but you cannot read a file backwards.
Actually, you could do it. First you would have to learn how the device containing the file worked. Let's suppose it were a hard disk. Then you would have to learn how it was controlled. Then you would have to learn how to redesign its circuits so that you could spin the disk backwards. Then you would have to devise some method to detect where the sectors were, since you would not be able to take advantage of the built-in mechanisms. Then, since you were reading the data backwards, "the" would not be "the". It wouldn't even be "eht", because all the bits would be reversed. Shall I go on, or would you like to eat some Cheeros, pump some energy to your brain, and expend it on some reasonable and sensible thought? |
Since you mentioned appending to a file:
If you intend to insert lines at the beginning, you will have to shift the existing contents down. This involves reading in and writing out everything already there, of course, so will take even longer as files get bigger. For reading a file backwards: Read each line and store them all in memory. You can then access them in what ever order you please. |
But that isn't reading a file backwards. That's reading it forward, and reading memory backwards. It's the right answer to an unasked question.
|
Often times the person asking doesn't know what to ask.
|
Funny, I thought reading a file backwards would be writing it. ;)
|
I said read the file backwards in terms of lines because I thought there was an effective method of doing so rather then loading to memory then reading from there which is slow. ^^'' Oops.
Quote:
|
Keiro, why not just process the lines of the file in reverse order, rather than try to append at the beginning? In other words, why is it required that you rearrange the file contents, rather than rearranging memory contents? Swapping memory around will be much faster, especially given that you only need to swap references to the read-in lines in most languages (not sure how it is in PHP, since I've never used it).
|
Quote:
|
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 Anyways, thanks for your help guys. |
| All times are GMT -5. The time now is 11:19 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC