CGI Development

Moderators: Jonathan
Number of threads: 55
Number of posts: 121

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Append to a file (from the beginning). Posted by InFeStEd on 10 Dec 2001 at 2:22 PM
Description:
I've created a tagger. They are new, I guess, and they are very stupid. What you do is put in text it and writes it to a global file where people will read. It's like mixing a chat and a messageboard.

Code description:
The code for my tagger appends to the file. But append only appends to the end.

Here is my current code
open(WRITEFILE, ">>$file_path");
sysseek(WRITEFILE,0,0)	;
print WRITEFILE "-> $FORM{'tag'}\n";
close(WRITEFILE);


Problem:
I need the program to append to the beginning of the file. Not the end. But sysseek or seek won't let you WRITE to the file, only read from the point you specify. How can I write to the beginning?

Thanks much.<br>
-Mike
{InFeStEd-ArCh0n}

Report
Re: Append to a file (from the beginning). Posted by DrGonzo on 21 Dec 2001 at 12:35 AM
heres a simple solution

lets say your chat file is called ChatLog.txt.

Copy ChatLog.txt to a file called ChatLog.old
Then open ChatLog.txt for a write and print the new text into it then open the .old file and print that text into the new ChatLog.txt.

want some code??

sub CreateOldFile
{
my $file_fame = $_[0];
my $newname = substr($file_name, 0, rindex($file_name ,'.')) . '.old';
my $file_data;

open(COFINPUTFILE, "< $file_name");
open(COFOUTPUTFILE, "> $newname");
chmod(0777, $newname);
while($file_data = <COFINPUTFILE>)
{
print COFOUTPUTFILE $file_data;
}
close(COFOUTPUTFILE);
close(COFINPUTFILE);

return($newname);
}
my $new_text = "new text or form value";
my $file_name = "ChatLog.txt";
my $oldfile_name = CreateOldFile($file_name); #Copys file to a file called $file_name.old and returns the name
my $file_data;

open(CHATLOG, "> $file_name");
open(OLDCHATLOG, "< $oldfile_name");
select CHATLOG; #pipe output to CHATLOG handle
print $new_text . "\n";
while($file_data = <OLDCHATLOG>)
{
print $file_data;
}
select STDOUT;
close(OLDCHATLOG);
close(CHATLOG);

If that code isn't extremely easy for you to understand, then use it as best you can and go get a good book on PERL.
Report
Re: Append to a file (from the beginning). Posted by rinmak on 7 Feb 2002 at 5:32 AM
hey,

there is an eaier way to do it, but DrGonzos way is a safer way, anyway the easy way is to read the whole file into a scalar, add what u want to the end and then write it out again, eg:

open(INFILE,"data.txt") || die;
while(<INFILE>) { $buffer .= $_; }
close(INFILE);

$buffer = "Put here what you want at the start\n" . $buffer;

open(OUTFILE,">data.txt") || die;
print OUTFILE $buffer;
close(OUTFILE);


sorry im on windoze at the moment so i cant test the code but it all looks right, just make sure that you use DrGonzos method when the file starts getting into the megabytes range :)
Report
Re: Append to a file (from the beginning). Posted by helihopper on 13 Jan 2009 at 5:35 PM
your code works but i may be doing something wrong
can u check it for me? i am getting extra
data printed out to the file:

http://pwinquist.com/blog.html

here is the perl:
#!/usr/local/bin/perl -w

print "Content-type:text/html\n\n";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

foreach $key (keys(%FORM)) {
"$key = $FORM{$key}\n";
}

#print MAIL "Hello Dudes . . .\n";

# print MAIL "$FORM{int1}\n";
# print MAIL "$FORM{int1}\n";

$now = localtime time;
# print "It is now $now\n";


$test1 = $FORM{comments};

$test2 = $FORM{name};

open(INFILE,"output7.txt") || die;
while(<INFILE>) { $buffer .= $_; }
close(INFILE);

$buffer = "\n\n$now\n$test2

says:\n$test1\n\n-------------------------------------------------------------------------------------------------------------------------------------\n\n" . $buffer;

open(OUTFILE,">output7.txt") || die;
print OUTFILE $buffer;
close(OUTFILE);

#reading
open(OUTFILE, "output7.txt") || die "Opening output7.txt: $!";
@test10=<OUTFILE>;
close(OUTFILE);

print <<EndHTML;

<html>
<head><title>Opinion</title> </head>
<body bgcolor=white>

Return to <a href="http://pwinquist.com/page1.html" target="_top">Las Vegas Sins & Scams</a>.

<table width=700><tr><td align=left>
<pre>
@test10 <br><br>
$buffer
</pre>
</td></tr></table>

Return to <a href="http://pwinquist.com/page1.html" target="_top">Las Vegas Sins & Scams</a>.

<hr width=100%>

</body>
</html>

EndHTML
;

sub dienice {
($errmsg) = @_;
print "<h2>Error</h2>\n";
print "$errmsg<p>\n";
print "</body></html>\n";
exit;
}


thanks
Report
Re: Append to a file (from the beginning). Posted by helihopper on 13 Jan 2009 at 5:35 PM
your code works but i may be doing something wrong
can u check it for me? i am getting extra
data printed out to the file:

http://pwinquist.com/blog.html

here is the perl:
#!/usr/local/bin/perl -w

print "Content-type:text/html\n\n";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

foreach $key (keys(%FORM)) {
"$key = $FORM{$key}\n";
}

#print MAIL "Hello Dudes . . .\n";

# print MAIL "$FORM{int1}\n";
# print MAIL "$FORM{int1}\n";

$now = localtime time;
# print "It is now $now\n";


$test1 = $FORM{comments};

$test2 = $FORM{name};

open(INFILE,"output7.txt") || die;
while(<INFILE>) { $buffer .= $_; }
close(INFILE);

$buffer = "\n\n$now\n$test2

says:\n$test1\n\n-------------------------------------------------------------------------------------------------------------------------------------\n\n" . $buffer;

open(OUTFILE,">output7.txt") || die;
print OUTFILE $buffer;
close(OUTFILE);

#reading
open(OUTFILE, "output7.txt") || die "Opening output7.txt: $!";
@test10=<OUTFILE>;
close(OUTFILE);

print <<EndHTML;

<html>
<head><title>Opinion</title> </head>
<body bgcolor=white>

Return to <a href="http://pwinquist.com/page1.html" target="_top">Las Vegas Sins & Scams</a>.

<table width=700><tr><td align=left>
<pre>
@test10 <br><br>
$buffer
</pre>
</td></tr></table>

Return to <a href="http://pwinquist.com/page1.html" target="_top">Las Vegas Sins & Scams</a>.

<hr width=100%>

</body>
</html>

EndHTML
;

sub dienice {
($errmsg) = @_;
print "<h2>Error</h2>\n";
print "$errmsg<p>\n";
print "</body></html>\n";
exit;
}


thanks
Report
Re: Append to a file (from the beginning). Posted by superjoe30 on 19 Sept 2002 at 2:43 PM

: I need the program to append to the beginning of the file. Not the end. But sysseek or seek won't let you WRITE to the file, only read from the point you specify. How can I write to the beginning?
:

You could also do this:
1.) read the file into an array
2.) use shift() to append it
3.) write the file

Code:
open(FH, $filetoappend) || die "Couldn't open $filetoappend: $!";
my @file = <FH>;
close(FH);
shift @file, $appenddata;
open(FH, ">$filetoappend") || die "Couldn't open $filetoappend: $!";
foreach (@file){
print FH $_;
}
close(FH);





~~Perl~~Visual Basic~~JavaScript~~
SuperJoe




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.