Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 13th, 2005, 10:30 AM   #1
Aphex_Twin
Newbie
 
Join Date: Mar 2005
Posts: 27
Rep Power: 0 Aphex_Twin is on a distinguished road
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:
Aphex_Twin is offline   Reply With Quote
Old Mar 13th, 2005, 2:23 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
change:
[php] if (fprintf ($handle, "%d", $myvalue) === FALSE) [/php]

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

tempest is offline   Reply With Quote
Old Mar 13th, 2005, 2:25 PM   #3
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
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]
__________________

tempest is offline   Reply With Quote
Old Mar 14th, 2005, 12:27 PM   #4
Aphex_Twin
Newbie
 
Join Date: Mar 2005
Posts: 27
Rep Power: 0 Aphex_Twin is on a distinguished road
Thank you, your suggestions seem to work
Aphex_Twin is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:49 PM.

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