Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   I need some help with files (http://www.programmingforums.org/showthread.php?t=2741)

Aphex_Twin Mar 13th, 2005 9:30 AM

I need some help with files
 
I seem to have hit a snag with PHP, if anyone can lend a hand...

This is the code I used:
:

<HTML>
<HEAD>
<TITLE>TESTING PHP</TITLE>
</HEAD>
<BODY>
<?php
        $filename = 'test.txt';
        $myvalue=0;

        if (!$handle = fopen($filename, 'r'))
        { echo "Cannot open file $filename"; exit; }
       
        $myvalue = fscanf ($handle, "%d");
        $myvalue++;
         
        fclose($handle);

        if (!$handle = fopen($filename, 'w'))
        { echo "Cannot open file $filename"; exit; }

        if (fprintf ($handle, "%d", $myvalue) === FALSE)
        { echo "Cannot write to file $filename"; exit; }
 
          echo "Success, wrote $myvalue to file $filename";
 
          fclose($handle);

?>
</BODY>
</HTML>



What I want to do is open a file called "test.txt", read the value stored inside (an integer), increment the integer and store it back into the file. It's a kind of counter.

The above code gives the following error:

Fatal error: Call to undefined function: fprintf() in C:\Program Files\BadBlue\PE\files\test.php on line 21


But the functions printf, fwrite and fread are working and PHP is functional, part for this fprintf thing :eek:

tempest Mar 13th, 2005 1:23 PM

change:
[php] if (fprintf ($handle, "%d", $myvalue) === FALSE) [/php]

to:
[php] if (!fwrite($handle, $myvalue)) [/php]

tempest Mar 13th, 2005 1:25 PM

Personally, i think an easier alternative would be...

[php]
function incFile($file) {
$val = intval(readfile($file));
fopen($file, "w") or die("Could not open \"{$file}\"!");
fwrite($file, $val+1);
fclose($file);
}

incFile("test.txt");
[/php]

Aphex_Twin Mar 14th, 2005 11:27 AM

Thank you, your suggestions seem to work


All times are GMT -5. The time now is 6:26 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC