Forum Moderators: coopster & phranque

Message Too Old, No Replies

adding 1 to and reading the final value

from a text file

         

lindajames

12:12 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



Hi,

I currently have a text file that has a number in it, i need to know how i can add 1 to the value of that number, save it and then display the value in the browser. To make it more easy to understand... lets say I have a text file called "number.txt". this text file contains the following number "234", now basically what i need a code to do is add 1 to that number and save it, so in this case the "number.txt" wll now have a value of "235" and finally i need to display the final number on the browser.

Any help would be appreciated.

Cheers
Linda

incywincy

12:16 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



Linda,

You will have to do some cgi scripting for this, server side include would be a good mechanism for pulling the result into your page.

Personally I would use a database for this sort of thing otherwise file corruption could occur if the file was being accessed by mutliple pages.

What scripting language will you use?

lindajames

12:19 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



the file wont be accessed by multiple pages, i want to do this in perl. any suggestions would be appreciated.

cheers
linda

Glacai

12:25 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



Hi Linda,

open(NUMBER, "<number.txt");
$num = <NUMBER>;
close(NUMBER);
$num++;
open(NUMBER, ">number.txt");
print NUMBER $num;
close(NUMBER);

Regards,
Marc.

lindajames

12:32 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



Thanx Marc,

I have one question though. incywincy mentioned that file corruption could occur if the file was being accessed by mutliple pages. what does he mean by that? if multiple users were view the data from that text file at the same time, could the file get corrupted? or is it if multiple users were trying to write to the file at the same time?

Cheers
Linda

netcommr

12:42 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



#!/usr/bin/perl
my $file = "/path/to/your/file.txt";
open(FILE,">$file");
my $data = <FILE>;
$data++;
print FILE $data;
close(FILE);
print $data;
exit;


-------------------- put this in html document
<!--#include virtual="/path/to/above/script.cgi" -->


NOTE:
believe incywincy, the above will get corrupted with many visitors or if page is accessed by more than one at the same exact time. There is no file locking on *nix machines. You must use a flock [search.cpan.org] to keep this from happening, but no always installed/available. You can make your own if flock not available. Just open a file for writing at the start of the script, just before that, check if the file exsists, loop/pause/quit if exsists, delete this file at the end...basically

Glacai

12:48 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



Yeah use flock, but I think what netcommr suggests would append to the end of file after reading? Not sure if there is a rewind, maybe I'm wrong?

lindajames

1:05 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



if i had a perl script that reads from a text file, and will be accessed by many people at the same time, would that corrupt the file? or is it just in the case of writing to the file?

netcommr

1:24 pm on Jul 16, 2003 (gmt 0)

10+ Year Member




Glacai, what you posted will work fine, I just took a shortcut but forgot a symbol, thanks for the heads up.
< = reading
> = writing, truncates the file
>> = append
+< = reading & writing (updating)
+> = same as above, but truncates file first (useless)

try changing the '>' to '+<':

open(FILE,"+<$file");

---------------
lindajames, you can have as many people reading a file without concerns, just when you write to them do you need to make sure you are the only one doing it at this time kinda thing...

lindajames

1:30 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



ok, so i guess file locking using flock is the answer to the file corruption problem. but what exactly does flock do and how exactly does it work? at the moment i have an application that uses text files to read and write to, but it doesnt use flock, how easy will it be to implement flock?

Cheers
Linda

lindajames

2:16 pm on Jul 16, 2003 (gmt 0)

10+ Year Member



im hosting on a Windows 2000 IIS server anyway, so do i need to do file locking using flock? or should it be ok?

Any suggestions would be appreciated.

Cheers
Linda