<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Append to a file (from the beginning).' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Append to a file (from the beginning).' posted on the 'CGI Development' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Wed, 19 Jun 2013 05:14:15 -0700</pubDate>
    <lastBuildDate>Wed, 19 Jun 2013 05:14:15 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Append to a file (from the beginning).</title>
      <link>http://www.programmersheaven.com/mb/CGI/90583/90583/append-to-a-file-from-the-beginning/</link>
      <description>&lt;strong&gt;Description&lt;/strong&gt;:&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Code description&lt;/strong&gt;:&lt;br /&gt;
The code for my tagger appends to the file.  But append only appends to the end.&lt;br /&gt;
&lt;br /&gt;
Here is my current code&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
open(WRITEFILE, "&amp;gt;&amp;gt;$file_path");
sysseek(WRITEFILE,0,0)	;
print WRITEFILE "-&amp;gt; $FORM{'tag'}\n";
close(WRITEFILE);&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Problem&lt;/strong&gt;:&lt;br /&gt;
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?&lt;br /&gt;
&lt;br /&gt;
Thanks much.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;strong&gt;-Mike&lt;br /&gt;
{InFeStEd-ArCh0n}&lt;/strong&gt;&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CGI/90583/90583/append-to-a-file-from-the-beginning/</guid>
      <pubDate>Mon, 10 Dec 2001 14:22:30 -0700</pubDate>
      <category>CGI Development</category>
    </item>
    <item>
      <title>Re: Append to a file (from the beginning).</title>
      <link>http://www.programmersheaven.com/mb/CGI/90583/92405/re-append-to-a-file-from-the-beginning/#92405</link>
      <description>heres a simple solution&lt;br /&gt;
&lt;br /&gt;
lets say your chat file is called ChatLog.txt.&lt;br /&gt;
&lt;br /&gt;
Copy ChatLog.txt to a file called ChatLog.old&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
want some code??&lt;br /&gt;
&lt;br /&gt;
sub CreateOldFile&lt;br /&gt;
{&lt;br /&gt;
    my $file_fame = $_[0];&lt;br /&gt;
    my $newname = substr($file_name, 0, rindex($file_name ,'.')) . '.old';&lt;br /&gt;
    my $file_data;&lt;br /&gt;
&lt;br /&gt;
    open(COFINPUTFILE, "&amp;lt; $file_name");&lt;br /&gt;
    open(COFOUTPUTFILE, "&amp;gt; $newname");&lt;br /&gt;
    chmod(0777, $newname);&lt;br /&gt;
      while($file_data = &amp;lt;COFINPUTFILE&amp;gt;)&lt;br /&gt;
      {&lt;br /&gt;
        print COFOUTPUTFILE $file_data;&lt;br /&gt;
      }&lt;br /&gt;
    close(COFOUTPUTFILE);&lt;br /&gt;
    close(COFINPUTFILE);&lt;br /&gt;
&lt;br /&gt;
    return($newname);&lt;br /&gt;
}&lt;br /&gt;
my $new_text = "new text or form value";&lt;br /&gt;
my $file_name = "ChatLog.txt";&lt;br /&gt;
my $oldfile_name = CreateOldFile($file_name); #Copys file to a file called $file_name.old and returns the name&lt;br /&gt;
my $file_data;&lt;br /&gt;
&lt;br /&gt;
open(CHATLOG, "&amp;gt; $file_name");&lt;br /&gt;
 open(OLDCHATLOG, "&amp;lt; $oldfile_name");&lt;br /&gt;
 select CHATLOG; #pipe output to CHATLOG handle&lt;br /&gt;
 print $new_text . "\n";&lt;br /&gt;
 while($file_data = &amp;lt;OLDCHATLOG&amp;gt;)&lt;br /&gt;
 {&lt;br /&gt;
 print $file_data;&lt;br /&gt;
 }&lt;br /&gt;
 select STDOUT;&lt;br /&gt;
 close(OLDCHATLOG);&lt;br /&gt;
close(CHATLOG);&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CGI/90583/92405/re-append-to-a-file-from-the-beginning/#92405</guid>
      <pubDate>Fri, 21 Dec 2001 00:35:35 -0700</pubDate>
      <category>CGI Development</category>
    </item>
    <item>
      <title>Re: Append to a file (from the beginning).</title>
      <link>http://www.programmersheaven.com/mb/CGI/90583/100502/re-append-to-a-file-from-the-beginning/#100502</link>
      <description>hey,&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
open(INFILE,"data.txt") || die;
while(&amp;lt;INFILE&amp;gt;) { $buffer .= $_; }
close(INFILE);

$buffer = "Put here what you want at the start\n" . $buffer;

open(OUTFILE,"&amp;gt;data.txt") || die;
print OUTFILE $buffer;
close(OUTFILE);
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
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 :)&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CGI/90583/100502/re-append-to-a-file-from-the-beginning/#100502</guid>
      <pubDate>Thu, 07 Feb 2002 05:32:26 -0700</pubDate>
      <category>CGI Development</category>
    </item>
    <item>
      <title>Re: Append to a file (from the beginning).</title>
      <link>http://www.programmersheaven.com/mb/CGI/90583/140183/re-append-to-a-file-from-the-beginning/#140183</link>
      <description>&lt;br /&gt;
: 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?&lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
You could also do this:&lt;br /&gt;
1.) read the file into an array&lt;br /&gt;
2.) use shift() to append it&lt;br /&gt;
3.) write the file&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Code:&lt;/strong&gt;&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
open(FH, $filetoappend) || die "Couldn't open $filetoappend: $!";
my @file = &amp;lt;FH&amp;gt;;
close(FH);
shift @file, $appenddata;
open(FH, "&amp;gt;$filetoappend") || die "Couldn't open $filetoappend: $!";
foreach (@file){
print FH $_;
}
close(FH);
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;&lt;br /&gt;
~~&lt;span style="color: Blue;"&gt;Perl&lt;/span&gt;~~&lt;span style="color: Red;"&gt;Visual Basic&lt;/span&gt;~~&lt;span style="color: Green;"&gt;JavaScript&lt;/span&gt;~~&lt;br /&gt;
SuperJoe&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CGI/90583/140183/re-append-to-a-file-from-the-beginning/#140183</guid>
      <pubDate>Thu, 19 Sep 2002 14:43:29 -0700</pubDate>
      <category>CGI Development</category>
    </item>
    <item>
      <title>Re: Append to a file (from the beginning).</title>
      <link>http://www.programmersheaven.com/mb/CGI/90583/384323/re-append-to-a-file-from-the-beginning/#384323</link>
      <description>your code works but i may be doing something wrong &lt;br /&gt;
can u check it for me? i am getting extra &lt;br /&gt;
data printed out to the file:&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://pwinquist.com/blog.html"&gt;http://pwinquist.com/blog.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
here is the perl:&lt;br /&gt;
#!/usr/local/bin/perl -w&lt;br /&gt;
&lt;br /&gt;
print "Content-type:text/html\n\n";&lt;br /&gt;
&lt;br /&gt;
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});&lt;br /&gt;
@pairs = split(/&amp;amp;/, $buffer);&lt;br /&gt;
foreach $pair (@pairs) {&lt;br /&gt;
($name, $value) = split(/=/, $pair);&lt;br /&gt;
$value =~ tr/+/ /;&lt;br /&gt;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;&lt;br /&gt;
$FORM{$name} = $value;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
foreach $key (keys(%FORM)) {&lt;br /&gt;
 "$key = $FORM{$key}\n";&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#print MAIL "Hello Dudes . . .\n";&lt;br /&gt;
&lt;br /&gt;
# print MAIL "$FORM{int1}\n";&lt;br /&gt;
# print MAIL "$FORM{int1}\n";&lt;br /&gt;
&lt;br /&gt;
        $now = localtime time;&lt;br /&gt;
        # print "It is now $now\n";&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$test1 = $FORM{comments};&lt;br /&gt;
&lt;br /&gt;
$test2 = $FORM{name};&lt;br /&gt;
&lt;br /&gt;
open(INFILE,"output7.txt") || die;&lt;br /&gt;
while(&amp;lt;INFILE&amp;gt;) { $buffer .= $_; }&lt;br /&gt;
close(INFILE);&lt;br /&gt;
&lt;br /&gt;
$buffer = "\n\n$now\n$test2 &lt;br /&gt;
&lt;br /&gt;
says:\n$test1\n\n-------------------------------------------------------------------------------------------------------------------------------------\n\n" . $buffer;&lt;br /&gt;
&lt;br /&gt;
open(OUTFILE,"&amp;gt;output7.txt") || die;&lt;br /&gt;
print OUTFILE $buffer;&lt;br /&gt;
close(OUTFILE);&lt;br /&gt;
&lt;br /&gt;
#reading&lt;br /&gt;
open(OUTFILE,  "output7.txt") || die "Opening output7.txt: $!";&lt;br /&gt;
@test10=&amp;lt;OUTFILE&amp;gt;;&lt;br /&gt;
close(OUTFILE);&lt;br /&gt;
&lt;br /&gt;
print &amp;lt;&amp;lt;EndHTML;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Opinion&amp;lt;/title&amp;gt;  &amp;lt;/head&amp;gt;  &lt;br /&gt;
&amp;lt;body bgcolor=white&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Return to &amp;lt;a href="http://pwinquist.com/page1.html" target="_top"&amp;gt;Las Vegas Sins &amp;amp; Scams&amp;lt;/a&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table width=700&amp;gt;&amp;lt;tr&amp;gt;&amp;lt;td align=left&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@test10 &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
$buffer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Return to &amp;lt;a href="http://pwinquist.com/page1.html" target="_top"&amp;gt;Las Vegas Sins &amp;amp; Scams&amp;lt;/a&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr width=100%&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
EndHTML&lt;br /&gt;
;&lt;br /&gt;
&lt;br /&gt;
sub dienice {&lt;br /&gt;
($errmsg) = @_;&lt;br /&gt;
print "&amp;lt;h2&amp;gt;Error&amp;lt;/h2&amp;gt;\n";&lt;br /&gt;
print "$errmsg&amp;lt;p&amp;gt;\n";&lt;br /&gt;
print "&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;\n";&lt;br /&gt;
exit;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    thanks&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CGI/90583/384323/re-append-to-a-file-from-the-beginning/#384323</guid>
      <pubDate>Tue, 13 Jan 2009 17:35:20 -0700</pubDate>
      <category>CGI Development</category>
    </item>
    <item>
      <title>Re: Append to a file (from the beginning).</title>
      <link>http://www.programmersheaven.com/mb/CGI/90583/384324/re-append-to-a-file-from-the-beginning/#384324</link>
      <description>your code works but i may be doing something wrong &lt;br /&gt;
can u check it for me? i am getting extra &lt;br /&gt;
data printed out to the file:&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://pwinquist.com/blog.html"&gt;http://pwinquist.com/blog.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
here is the perl:&lt;br /&gt;
#!/usr/local/bin/perl -w&lt;br /&gt;
&lt;br /&gt;
print "Content-type:text/html\n\n";&lt;br /&gt;
&lt;br /&gt;
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});&lt;br /&gt;
@pairs = split(/&amp;amp;/, $buffer);&lt;br /&gt;
foreach $pair (@pairs) {&lt;br /&gt;
($name, $value) = split(/=/, $pair);&lt;br /&gt;
$value =~ tr/+/ /;&lt;br /&gt;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;&lt;br /&gt;
$FORM{$name} = $value;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
foreach $key (keys(%FORM)) {&lt;br /&gt;
 "$key = $FORM{$key}\n";&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#print MAIL "Hello Dudes . . .\n";&lt;br /&gt;
&lt;br /&gt;
# print MAIL "$FORM{int1}\n";&lt;br /&gt;
# print MAIL "$FORM{int1}\n";&lt;br /&gt;
&lt;br /&gt;
        $now = localtime time;&lt;br /&gt;
        # print "It is now $now\n";&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$test1 = $FORM{comments};&lt;br /&gt;
&lt;br /&gt;
$test2 = $FORM{name};&lt;br /&gt;
&lt;br /&gt;
open(INFILE,"output7.txt") || die;&lt;br /&gt;
while(&amp;lt;INFILE&amp;gt;) { $buffer .= $_; }&lt;br /&gt;
close(INFILE);&lt;br /&gt;
&lt;br /&gt;
$buffer = "\n\n$now\n$test2 &lt;br /&gt;
&lt;br /&gt;
says:\n$test1\n\n-------------------------------------------------------------------------------------------------------------------------------------\n\n" . $buffer;&lt;br /&gt;
&lt;br /&gt;
open(OUTFILE,"&amp;gt;output7.txt") || die;&lt;br /&gt;
print OUTFILE $buffer;&lt;br /&gt;
close(OUTFILE);&lt;br /&gt;
&lt;br /&gt;
#reading&lt;br /&gt;
open(OUTFILE,  "output7.txt") || die "Opening output7.txt: $!";&lt;br /&gt;
@test10=&amp;lt;OUTFILE&amp;gt;;&lt;br /&gt;
close(OUTFILE);&lt;br /&gt;
&lt;br /&gt;
print &amp;lt;&amp;lt;EndHTML;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Opinion&amp;lt;/title&amp;gt;  &amp;lt;/head&amp;gt;  &lt;br /&gt;
&amp;lt;body bgcolor=white&amp;gt;  &lt;br /&gt;
&lt;br /&gt;
Return to &amp;lt;a href="http://pwinquist.com/page1.html" target="_top"&amp;gt;Las Vegas Sins &amp;amp; Scams&amp;lt;/a&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;table width=700&amp;gt;&amp;lt;tr&amp;gt;&amp;lt;td align=left&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@test10 &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
$buffer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&amp;lt;/table&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Return to &amp;lt;a href="http://pwinquist.com/page1.html" target="_top"&amp;gt;Las Vegas Sins &amp;amp; Scams&amp;lt;/a&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;hr width=100%&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
EndHTML&lt;br /&gt;
;&lt;br /&gt;
&lt;br /&gt;
sub dienice {&lt;br /&gt;
($errmsg) = @_;&lt;br /&gt;
print "&amp;lt;h2&amp;gt;Error&amp;lt;/h2&amp;gt;\n";&lt;br /&gt;
print "$errmsg&amp;lt;p&amp;gt;\n";&lt;br /&gt;
print "&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;\n";&lt;br /&gt;
exit;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    thanks&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/CGI/90583/384324/re-append-to-a-file-from-the-beginning/#384324</guid>
      <pubDate>Tue, 13 Jan 2009 17:35:48 -0700</pubDate>
      <category>CGI Development</category>
    </item>
  </channel>
</rss>