By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,245 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,245 IT Pros & Developers. It's quick & easy.

how to append to a text file?

100+
P: 170
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?
May 8 '08 #1
Share this Question
Share on Google+
2 Replies


nithinpes
Expert 100+
P: 410
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.
Expand|Select|Wrap|Line Numbers
  1. open(IN,">>history.log");
  2. print IN "This is the new line appended\n"; ## will append to the end
  3.  
If your purpose is to get/display the last line before writing further to the file, you need to open it for read & write.
Expand|Select|Wrap|Line Numbers
  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);
  6.  
May 8 '08 #2

100+
P: 170
cool..thanks a lot
:)
May 8 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.