Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 12th, 2007, 4:51 PM   #1
Keiro
Newbie
 
Join Date: Nov 2006
Location: Here!!!
Posts: 7
Rep Power: 0 Keiro is on a distinguished road
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
Keiro is offline   Reply With Quote
Old Aug 12th, 2007, 6:35 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Aug 12th, 2007, 6:48 PM   #3
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Aug 12th, 2007, 7:04 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Aug 12th, 2007, 10:41 PM   #5
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Often times the person asking doesn't know what to ask.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Aug 13th, 2007, 2:35 AM   #6
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
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
lectricpharaoh is offline   Reply With Quote
Old Aug 13th, 2007, 3:09 AM   #7
Keiro
Newbie
 
Join Date: Nov 2006
Location: Here!!!
Posts: 7
Rep Power: 0 Keiro is on a distinguished road
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:
Originally Posted by Dameon View Post
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.
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?
Keiro is offline   Reply With Quote
Old Aug 13th, 2007, 4:05 AM   #8
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5 lectricpharaoh will become famous soon enough
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
lectricpharaoh is offline   Reply With Quote
Old Aug 13th, 2007, 7:04 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
rather then loading to memory then reading from there which is slow.
I'm sorry, but you don't know what you're talking about. File operations are orders of magnitude slower than memory operations.
__________________
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
DaWei is offline   Reply With Quote
Old Aug 13th, 2007, 9:21 AM   #10
Keiro
Newbie
 
Join Date: Nov 2006
Location: Here!!!
Posts: 7
Rep Power: 0 Keiro is on a distinguished road
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.
Keiro is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:07 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC