thesmart1
08-06-2007, 02:55 AM
I have a script that reads two numbers (separated by a comma) from a txt file and adds one to one or both of the numbers. It then writes the new numbers back to the file, in the original format.
It works, however, it keeps adding spaces to the beginning of the file. Normally this wouldn't be a major problem, but this file will eventually be modified a lot of times. That could eventually make the file size huge, slowing down the script.
Here is the script (there's more to the PHP file, but this is what I'm having trouble with):
// OPEN AND READ THE FILE
$stats_file=fopen($stats_file_name,'r+');
$stats=trim(fread($stats_file,filesize($stats_file_name)));
// PARSE AND ADD 1 WHERE APPROPRIATE
$stats_values=explode(',',$stats);
$stats_values[0]++;
if ($is_unique==true){
$stats_values[1]++;
}
// WRITE NEW STATS TO FILE
$new_stats=trim($stats_values[0].','.$stats_values[1]);
$new_stats=str_replace(' ','',$new_stats);
ftruncate($stats_file,0);
fwrite($stats_file,$new_stats);
fclose($stats_file);
It works, however, it keeps adding spaces to the beginning of the file. Normally this wouldn't be a major problem, but this file will eventually be modified a lot of times. That could eventually make the file size huge, slowing down the script.
Here is the script (there's more to the PHP file, but this is what I'm having trouble with):
// OPEN AND READ THE FILE
$stats_file=fopen($stats_file_name,'r+');
$stats=trim(fread($stats_file,filesize($stats_file_name)));
// PARSE AND ADD 1 WHERE APPROPRIATE
$stats_values=explode(',',$stats);
$stats_values[0]++;
if ($is_unique==true){
$stats_values[1]++;
}
// WRITE NEW STATS TO FILE
$new_stats=trim($stats_values[0].','.$stats_values[1]);
$new_stats=str_replace(' ','',$new_stats);
ftruncate($stats_file,0);
fwrite($stats_file,$new_stats);
fclose($stats_file);