Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Appending to front of a file?

Status
Not open for further replies.

Cake

Programmer
Oct 22, 2000
12
US
Hi,
Instead of doing a sorting method, I was wondering if it's possible to open a file for appending to the *front* instead of the end?

ie. the following appends to the end of the file:

Code:
open(FILE, ">>database.txt");
print FILE "$topic§$currentdate§$message\n";
close(FILE);

Is there a file operator that will send the output to the front (without overwriting the file) besides the operator ">>" that sends the output to the end?

Thanks,
Cake.
 
try this:

open (FILE, ">>database.txt") or die "Couldn't open file database.txt: $!";
seek (FILE,0,0);
print FILE "$tpic $currentdate $message\n";
close (FILE);

seek (handle, offset whence) resets the file pointer, 0 puts it the the begining of the file (FILE,0,2) puts it to the end of the file.

You could also reverse the file add what you want, in reverse, and then forward the file and you should have what you want in the front.

Miah
 
All that will do is overwrite whatever is at the beginning of the file. Unless there has been a major change in the way things work, the only way I know of to prepend to (AKA "append to the beginning of") a file is to write the data you want to prepend and then copy the rest of the file after that. I don't believe that opening a file for appending and setting the pointer to the beginning of the file will do what you want it to.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top