Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Appending to front of a file?

Appending to front of a file?

Appending to front of a file?

   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:

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

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?


RE: Appending to front of a file?

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.  


RE: Appending to front of a file?

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close