Click to See Complete Forum and Search --> : How to add data to a file starting from the top?


taintedkiller
09-13-2003, 08:17 PM
I'm writing my first news system but I'm kinda stuck. I've already searched everywhere for this and no results. How do I add data to top of the file instead of the bottom? Thanks.

-Randy

Scriptage
09-13-2003, 08:33 PM
try:

open(F, "+<blah.txt") || die "Some sort of error: $!";
seek(F, 0, 0);
print F "Some text at start of file";
close(F);

taintedkiller
09-13-2003, 09:58 PM
It shows up all weird. Half the text in the previous news are usually over written with a new one. Is there something wrong with the code? Thanks for the help.

Scriptage
09-13-2003, 10:05 PM
sorry mate change this line:

open(F, "+<blah.txt") || die "Some sort of error: $!";

to

open(F, ">>blah.txt") || die "Some sort of error: $!";

and that works I've just tested it

taintedkiller
09-13-2003, 10:45 PM
OK, it works but it doesn't start at top like I wanted to. I want to be able to add news to a .txt file using an html form page but newest entries go first.

I have the code like this:

-------------------
open(DAT,">>$newsfile") || die "Some sort of error: $!";

seek (DAT, 0, 0);
print DAT "<b>$subject</b><br>\n";
print DAT "$thenews\n";
print DAT "<br><br>\n";
print DAT "-$username<br><br>\n";

close(DAT);
-------------------

-Randy

Thanks for helping though.

Scriptage
09-14-2003, 12:18 AM
seek(DAT, 0, 0);
should go to the start of the file, I don't understand why it isn't.
I'll figure it out soon ;)

Jeff Mott
09-14-2003, 12:28 AM
taintedkiller, to write to the beginning of the file while still keeping what was there you'd have to read all the old content in and then write everything back to the file again with your new data at the beginning.

CyCo
09-14-2003, 10:20 AM
taintedkiller, there are many ways to accomplish what you want, here is one method:

open the file for reading, then close it
then open it again for writing...using the unshift function to add data to the top line of the file...

open(DAT,"$newsfile") || die "Some sort of error: $!";
@lines = <DAT>;
close(DAT);

open(DAT,">$newsfile") || die "Some sort of error: $!";
unshift(@lines, "<b>$subject</b><br>\n$thenews\n<br><br>\n-$username<br><br>\n");
foreach $line(@lines) {
print DAT $line;
}
close(DAT);

taintedkiller
09-14-2003, 10:36 AM
Hi cyco, I think I found an easier way. Instead of using the unshift command, I just print.


open(DAT,"$newsfile") || die "Some sort of error: $!";
@oldnews = <DAT>;
close(DAT);

open(DAT,">$newsfile") || die "Some sort of error: $!";
print DAT "<b>$subject</b><br>\n";
print DAT "$thenews\n";
print DAT "<br><br>\n";
print DAT "-$username<br><br>\n";
print DAT @oldnews;
close(F);


I'm not sure if that way is better though.

Do you know how I can limit the number of entries shown? I only want 2 news entry to be shown on the page and the rest goes to an archieve page. Is that possible with PERL?

Thanks guys,

-Randy

CyCo
09-14-2003, 10:59 AM
pretty much the same idea...I've just eliminated more print statements...

Jeff Mott
09-14-2003, 12:29 PM
Most importantly, though, is you need to lock the file when reading and writing. Otherwise you risk your data becoming corrupted.use Fcntl qw[:flock :seek];

open(DAT, $newsfile) or die $!;
flock(DAT, LOCK_EX) or die $!;
my @oldnews = <DAT>;
seek(DAT, 0, SEEK_SET) or die $!;
print DAT "<b>$subject</b><br>\n",
"$thenews\n",
"<br><br>\n",
"-$username<br><br>\n",
@oldnews;
truncate(DAT, 0) or die $!;
close(DAT) or die $!;Do you know how I can limit the number of entries shown? I only want 2 news entry to be shown on the page and the rest goes to an archieve page.I can't tell what character(s) you use as your separator between entries so you'll have to fill that part in.use Fcntl qw[:flock];

open(DAT, $newsfile) or die $!;
flock(DAT, LOCK_SH) or die $!;

for (1 .. 2) {
local $/ = "\n\n"; # <- change this
my $entry = <DAT>;
# ...
}

close(DAT) or die $!;

taintedkiller
09-14-2003, 01:16 PM
I can't tell what character(s) you use as your separator between entries so you'll have to fill that part in.
I seperate entries using a Paragraph Seperator <P>.

I got the file locked now. Thanks for letting me know.