Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

how to append to a text file?

Question posted by: poolboi (Familiar Sight) on May 8th, 2008 02:51 AM
hi guys,

i've got a problem in appending text file

i'm creating a history log for people who use my my programs
in my text file i got

root Thu May 8 09:38:56 2008 Commands used: MIO:IMSI;

hm...any idea how i can find the last line written
so that i can append to the text file by writing to the next line?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
nithinpes's Avatar
nithinpes
Expert
253 Posts
May 8th, 2008
04:54 AM
#2

Re: how to append to a text file?
Quote:
Originally Posted by poolboi
hi guys,

i've got a problem in appending text file

i'm creating a history log for people who use my my programs
in my text file i got

root Thu May 8 09:38:56 2008 Commands used: MIO:IMSI;

hm...any idea how i can find the last line written
so that i can append to the text file by writing to the next line?


If you open a file for appending, the lines written to the file further will be appended to the end. You need not worry about finding the last line number and writing to next line.
Code: ( text )
  1. open(IN,">>history.log");
  2. print IN "This is the new line appended\n"; ## will append to the end


If your purpose is to get/display the last line before writing further to the file, you need to open it for read & write.
Code: ( text )
  1. open(IN,"+<history.log");
  2. @lines=<IN>;
  3. print "last line is:\n $lines[$#lines]\n";
  4. print IN "This is the new line appended\n";
  5. close(IN);

Reply
poolboi's Avatar
poolboi
Familiar Sight
170 Posts
May 8th, 2008
06:41 AM
#3

Re: how to append to a text file?
cool..thanks a lot
:)

Reply
Reply
Not the answer you were looking for? Post your question . . .
182,266 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Perl Forum Contributors