Connecting Tech Pros Worldwide Help | Site Map

Basic fopen question

deko
Guest
 
Posts: n/a
#1: Jul 17 '05
I'm stuck on a very basic counter program. I have a file that looks like this:

4
5
7
8
2
9

I want to periodically increment the top line value by one, without affecting
any other line in the file.

$vct='counter.txt';
$fp=fopen($vct, 'r+'); //from what I read in the manual, r+ is the right
choice...
$oldct=fgets($fp); //get the first line
$newct = $oldct+1; //increment value
fwrite($fp, $newct); //write new value over old value
fclose($fp);

why doesn't this work????


Garp
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Basic fopen question



"deko" <dje422@hotmail.com> wrote in message
news:Pib7c.26768$vL6.12700@newssvr29.news.prodigy. com...[color=blue]
> I'm stuck on a very basic counter program. I have a file that looks like[/color]
this:[color=blue]
>
> 4
> 5
> 7
> 8
> 2
> 9
>
> I want to periodically increment the top line value by one, without[/color]
affecting[color=blue]
> any other line in the file.
>
> $vct='counter.txt';
> $fp=fopen($vct, 'r+'); //from what I read in the manual, r+ is the right
> choice...
> $oldct=fgets($fp); //get the first line
> $newct = $oldct+1; //increment value
> fwrite($fp, $newct); //write new value over old value
> fclose($fp);
>
> why doesn't this work????[/color]

That particular code doesn't work because fgets() is moving your file
pointer. Reset it to the top with fseek(). Good luck when you get that
number to 2 digits, though.

Or get a database. 8)

Garp


Closed Thread