你是说,下一次用这个文件时总是一开始就把文件大小截为零,然后从头开始写,但问题是在下一次调用前,应该内容还是存在的吧?我只是调用一次啊,怎么最后内容都空的呢?我把代码全部帖上<?php
class ipdata
{
var $ip;
var $amount;
var $next;
};class ipdata_functions
{
var $head;
var $p;
var $tail; 
function __destruct()
{
if($this->head!=NULL)
{
$this->p=new ipdata();
$this->p->next=NULL;
$this->p->amount=0;

$this->p=$this->head;
$file=fopen("ip.txt","w");
//fseek($file,0);
while($this->p!=NULL)
{
fwrite($file,$this->p->ip);
fwrite($file," ");
fwrite($file,$this->p->amount);
fwrite($file," ");
$this->p=$this->p->next;
}
fclose($file);
}

}
function __construct()
{
$this->head=new ipdata();
$this->head->next=NULL;
 
$this->tail=new ipdata();
$this->tail->next=NULL;
if (!file_exists("ip.dat"))
{
$file=fopen("ip.dat","r+");
fseek($file,0);
$ch=fgetc($file);
while(!feof($file))
{
$this->p=new ipdata();
$this->p->amount=0;
$this->p->next=NULL;

while($ch!=" ")
{
$this->p->ip.=$ch;
}
$ch=fgetc($file);
$this->p->amount=fgetc($file);
if($this->head==NULL) $this->head=$this->p;
else
{
$this->tail->next=$this->p;
$this->tail=$this->p;
}
$ch=fgetc($file);
}
}

}

function checkandrecord($clientIP)
{
if ($this->head==NULL)
echo "到目前为止还没有投票哦!";
else
{
$this->p=new ipdata();
$this->p->amount=0;
$this->p->next=NULL;

$this->p=$this->head;
while($this->p!=NULL && strcmp($this->p->ip,$clientIP))
{
$this->p=$this->p->next;
}

if($this->p==NULL)
{
$this->p->ip=$clientIP;
$this->p->amount=1;
$this->next=NULL;

//找出尾指针
$this->tail=$this->head;
while($this->tail!=NULL)
{
$this->tail=$this->tail->next;
}

//链接到尾指针去

$this->tail->next=$this;
}
else
{
if ($this->p->amount>=3)
echo "感谢你的投票,你已经投票过了,请不要反复投票!";
else 
{
$this->p->amount++;
}
}


}

} function creat()
{
$a[0]=321412;
$a[1]=454354;
$a[2]=433676;
//$p=new ipdata(); $this->p=new ipdata();
$this->p->amount=0;
$this->p->next=NULL;

$this->p->ip=$a[0];
$this->p->amount=1;
$this->head=$this->p; $this->p=new ipdata_functions();
$this->p->next=NULL;
$this->p->ip=$a[1];
$this->p->amount=2;
$this->head->next=$this->p;

$this->p=new ipdata_functions();
$this->p->next=NULL;
$this->p->ip=$a[2];
$this->p->amount=3;
$this->head->next->next=$this->p;
}
function show()
{
if($this->head!=NULL)
{
$this->p=new ipdata();
$this->p->amount=0;
$this->p->next=NULL;

$this->p=$this->head;
while($this->p!=NULL)
{
echo $this->p->ip;
echo " ";
echo $this->p->amount;
echo "<br>";
$this->p=$this->p->next;
}
}
}
}$temp =new ipdata_functions();
$temp->creat();
$temp->show();
$temp=NULL;?>

解决方案 »

  1.   

    问题解决了,还是靠自己一步一步跟踪程序才知道那里出错了.原来:$this->p=new ipdata();
    $this->p->amount=0;
    $this->p->next=NULL;$this->p->ip=$a[0];
    $this->p->amount=1;
    $this->head=$this->p;$this->p=new ipdata_functions();   //应该是ipdata();
    $this->p->next=NULL;
    $this->p->ip=$a[1];
    $this->p->amount=2;
    $this->head->next=$this->p;$this->p=new ipdata_functions();   //应该是ipdata()
    $this->p->next=NULL;
    $this->p->ip=$a[2];
    $this->p->amount=3;
    $this->head->next->next=$this->p;
    正因为不小心把上面的写错了,就建立了好几个ipdata_functions对象,因此最后执行了多次的构造与析构函数.我晕,我还以为是php的问题,原来是自己的问题.太不小心了.