Append to a file (from the beginning).

[b]Description[/b]:
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.

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

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

[b]Problem[/b]:
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.

[b]-Mike
{InFeStEd-ArCh0n}[/b]

Comments

  • 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 = )
    {
    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 . "
    ";
    while($file_data = )
    {
    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.
  • 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:

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

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

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

    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 :)

  • : 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

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


    [hr]
    ~~[blue]Perl[/blue]~~[red]Visual Basic[/red]~~[green]JavaScript[/green]~~
    SuperJoe

  • 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

    ";

    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}
    ";
    }

    #print MAIL "Hello Dudes . . .
    ";

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

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


    $test1 = $FORM{comments};

    $test2 = $FORM{name};

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

    $buffer = "

    $now
    $test2

    says:
    $test1

    -------------------------------------------------------------------------------------------------------------------------------------

    " . $buffer;

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

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

    print
    Opinion


    Return to Las Vegas Sins & Scams.

    @test10


    $buffer
    Return to Las Vegas Sins & Scams.







    EndHTML
    ;

    sub dienice {
    ($errmsg) = @_;
    print "

    Error

    ";
    print "$errmsg


    ";
    print "
    ";
    exit;
    }


    thanks

  • 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

    ";

    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}
    ";
    }

    #print MAIL "Hello Dudes . . .
    ";

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

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


    $test1 = $FORM{comments};

    $test2 = $FORM{name};

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

    $buffer = "

    $now
    $test2

    says:
    $test1

    -------------------------------------------------------------------------------------------------------------------------------------

    " . $buffer;

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

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

    print
    Opinion


    Return to Las Vegas Sins & Scams.

    @test10


    $buffer
    Return to Las Vegas Sins & Scams.







    EndHTML
    ;

    sub dienice {
    ($errmsg) = @_;
    print "

    Error

    ";
    print "$errmsg


    ";
    print "
    ";
    exit;
    }


    thanks

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories