Connecting Tech Pros Worldwide Help | Site Map

Basic fopen question

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 04:19 AM
deko
Guest
 
Posts: n/a
Default Basic fopen question

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????



  #2  
Old July 17th, 2005, 04:19 AM
Garp
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.