CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Perl/ CGI (http://www.codingforums.com/forumdisplay.php?f=5)
-   -   So simple a perl question i'm almost embarrassed to ask. (Print text to TOP of file). (http://www.codingforums.com/showthread.php?t=2100)

sarah_anne 07-16-2002 01:21 PM

So simple a perl question i'm almost embarrassed to ask. (Print text to TOP of file).
 
OK, to append it's something like:


$mystuff= $formdata{'mine'};
$inputstuff = $formdata{'inputtext'};
open (LOG, ">>stuff.txt") || &ErrorMessage;

and to wipe clean and overwrite it's

$mystuff= $formdata{'mine'};
$inputstuff = $formdata{'inputtext'};
open (LOG, ">stuff.txt") || &ErrorMessage;

but.... how do i make it so that input from a simple form goes to the TOP of my text file whilst keeping everything else that's there? Is there something simple to write instead? or does it involve harder work than it sounds? It seems as if it should be something I already know, but unfortunatly it's either too simple to document on the internet that i've searched so far or no one else ever needs to do this?? :S

Sarah.

chrisvmarle 07-16-2002 02:27 PM

Here's what I use for problems like this:

Code:

$mystuff= $formdata{'mine'};
$inputstuff = $formdata{'inputtext'};
open (LOG, "stuff.txt") || &ErrorMessage;
@data = <LOG>;
close (LOG);

open (LOG, ">stuff.txt") || &ErrorMessage;
print LOG "String or whatever to write to the top of the file";
foreach (@data) {
        print LOG $_;
}
close(LOG);

Mzzl, Chris

sarah_anne 07-16-2002 02:36 PM

aaah, i see how you're doing it... taking it all out of the file, writing the stuff i need to put at the top and then rewriting everything that was already there afterwards. guess they never make it simple, eh?

Anyway, thanks! :)

chrisvmarle 07-16-2002 04:22 PM

I was told lately why there things like this in Perl; the idea is, they give you everything you need and the rest is up to you.

(They give you the ability to write to files, but you have to make it write to the top of the file ;))

Mzzl, Chris


All times are GMT +1. The time now is 08:27 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.