View Full Version : Writting new lines in the begining of databases
ACJavascript
07-30-2003, 07:08 PM
Hey all,
Okay this is driving me insane!
I can't get a database file to save the way I want it.
Okay you know how you flock a file so that you save on the next line in the database.
flock(update,2);
seek(update,0,0);
print ect...
Now how do I make it so that the file will save the new line in the begining of the database.
For example:
Aaron
Sarah
Nathan
Now I want to save John (the new user)
John
Aaron
Sarah
Nathan
like that. Not
Aaron
Sarah
Nathan
John
I hope one of ya has the answer because My headache is to big lol
Thanks in advance :D
YUPAPA
08-01-2003, 12:51 AM
#!/usr/bin/perl
use Fcntl qw(:DEFAULT :flock);
use strict;
my $database = 'my_database.db';
my $database_bak = 'my_database.db.bak';
my $data = 'A Record here';
sysopen(W_FILE,$database_bak,O_WRONLY|O_CREAT|O_TRUNC, 0600) or die print "Fail to SYSOPEN $database_bak: $!";
flock(W_FILE, LOCK_EX) or die print "Fail to EX-LOCK $database_bak: $!";
print W_FILE $data."\n";
sysopen(R_FILE,$database,O_RDONLY|O_CREAT,0600) or die print "Fail to SYSOPEN $database: $!";
flock(R_FILE, LOCK_SH) or die print "Fail to SH-LOCK $database: $!";
while(<R_FILE>) {
print W_FILE $_;
}
flock(R_FILE, LOCK_UN) or die print "Fail to UN-LOCK $database: $!";
close(R_FILE) or die print "Fail to close $database: $!";
flock(W_FILE, LOCK_UN) or die print "Fail to UN-LOCK $database_bak: $!";
close(W_FILE) or die print "Fail to close $database_bak: $!";
rename($database_bak,$database);
unlink($database_bak);
ACJavascript
08-01-2003, 02:36 AM
COOL! thanks..
heres antoher one though. How would you do it in normal perl?
not using CPAN and CGI.pm
YUPAPA
08-01-2003, 03:25 AM
There is no mention of CPAN and CGI.pm there that i can see, and Fcntl & strict are VERY standard modules (come as standard, no need to add them to any perl5+)
ACJavascript
08-01-2003, 05:00 AM
Yea i see what ya mean.
Well then um,, I mean more like this
open(data,"FILE") || print "CAn't";
flock(data,2);
seek(data,0,0);
print data "bla";
close(data);
I thought it had to do with seek(data,0,0);
seek (0,0) puts it at the start of the file. So i've tried 2,2. 0,2. 2,0 ect....
No luck.
Well hope that explains it a bit better..
Thanks for the responses! :D
Pearl
08-01-2003, 08:51 AM
If you're sort of new to perl.. YUPAPA's way is quite scary to process in terms of doing it simply --- the way you want to do it :)
YUPAPA basically showed you one way to insert the new line at the beginning by opening a temp file adding your new line, appending the rest of your original file under it, then renaming the temporary file to the original file name. Although there are simplier ways to code this, the idea is the same and used quite commonly. Also YUPAPA's suggestion to use Fcntl to flock your files is a method that I suggest to use as well -- it's worth considering ;) It can be used like this as well:
use Fcntl qw(:flock);
open FILE , "foo.txt" or die "Couldn't open file";
flock FILE , LOCK_SH;
print <FILE>;
flock FILE , LOCK_UN;
close FILE;
CONSTANTS:
LOCK_SH - Shared
LOCK_EX - Exclusive
LOCK_NB - Non-blocking
LOCK_UN - Unlock
For more info on flock, seek and others, this is a handy link to bookmark: http://www.perldoc.com/perl5.6/pod/perlfunc.html#Alphabetical-Listing-of-Perl-Functions
As for the "easier" code you were looking for, try:
open(FILE,"+<foo.txt");
@lines = <FILE>;
seek(FILE,0,0);
#truncate(FILE,0); # uncomment to use this
print FILE "John\n";
print FILE @lines;
close(FILE);
The above example doesn't have any error checking or flocking, it's just the code, so you will have to add them.
Best of Luck.
ACJavascript
08-01-2003, 05:19 PM
Hey Pearl,
Well I did understand what yuppas code was doing I just have a script that uses this type of syntax: $name="bla"
not my $name="bla";
I know it doens't make that much of a difference you can even combine the two I just was wondering how to do it in those terms.
THanks for the Codes both of ya :D
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.