ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
well that sounds logical to me.
Does My count loop look right?
While messing around with the code I was able to delete everything in the file with truncate() after opening with ">>$DATFILE".
I also added the $N used as a counter in the code I posted just to see if that was counting up but it never seemed to.
it may be that i collected the value for $N at the wrong time.
gonna try ">$DATFILE" and see what happens.
thanks
As far as i know (from my own use) truncate receives a position in bytes from which to truncate the file from. So if you pass it "1" it will get rid of everything past the first byte. So you'll need the seek() and tell() functions to do what you want.
A quick test that i wrote:
Code:
open FILE, "+<data.txt"; # reading and other options
my @all= <FILE>; # just to know the total number of lines through $#all(index of last element)
seek(FILE, 0, 0); # go back to beginning of file
my $count=0;
while(<FILE>) # go line by line through the file
{
if($count==$#all-1) # if you are at the second last line
{
truncate(FILE, tell(FILE)); # get the current position in file (we are at the start of the last line) and truncate all from that position
}
$count++; # increment
}
I'm sure there are better ways to achieve the same thing, but it works, so i thought i'd post it anyway.
Hope this helps
Last edited by coolman0stress; 09-01-2003 at 09:14 AM.
well I tried Satan's last snippet. and it erased everything in the file.
coolman's suggestion did not do anything.
I'm at a bit of a loss. what is "$#N" versus "$N"
I think i like opening the file, reading all lines to @DATA and then overwriting the file and printing all the lines back but the last one.
Thats what satan was going for i think.
I'm gonna crack this nut somehow.
thanks for your help so far.
the problem was that the script that i wrote to add lines to the file
was putting everything all on the same line. I'm a fool
what i had come up with originally to delete lines also works.
alls well that ends well.
later on
scott
yeah i just came to the same conclusion.
I still have the problem that when i add to the text file from my script
everything winds up on the same line. here is the routine for adding to the file.
my $TEXT = param("itext");
open(DATFILE,">>$DATFILE") || ($MESG = "could not open $DATFILE");
flock(DATFILE, $LOCK_EXCLUSIVE);
print DATFILE '<p>';
print DATFILE "$TEXT";
print DATFILE '</p>';
$MESG = ' text added successfully';
close(DATFILE);
this is run from a form. and then the whole script is reloaded
then when i use the form again, it goes on the same line as the previous.
thanks alot for all you help. slowly starting to get a handle on all of this.
your assuption is correct.
now i guess i'll have to figure out how to use
chop() so that when i read the file into my HTML i don't see the
"\n"?
thanks a million
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.