![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2006
Location: Here!!!
Posts: 7
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
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.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Often times the person asking doesn't know what to ask.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#6 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5
![]() |
Funny, I thought reading a file backwards would be writing it.
![]()
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Nov 2006
Location: Here!!!
Posts: 7
Rep Power: 0
![]() |
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.
Ah, now thats more like it. Shifting lines down then added the new content at the begining. Is there a way to do that instead? |
|
|
|
|
|
#8 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5
![]() |
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).
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
#9 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: Nov 2006
Location: Here!!!
Posts: 7
Rep Power: 0
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| OnlineTextEditor.Com! | Sane | Show Off Your Open Source Projects | 43 | Jun 16th, 2006 8:55 AM |
| Read and write to one file | nnxion | C | 3 | Apr 11th, 2006 5:10 PM |
| How to read a line of from a file | aznluvsmc | C | 12 | Oct 22nd, 2005 5:36 PM |
| After execution - Error cannot locate /Skin File? | wchar | Visual Basic | 1 | Mar 5th, 2005 9:04 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |