Simulate C's data structure with PHP

Source: Internet
Author: User
Tags definition mail reset strlen

C is convenient for storing with a fixed data structure.
struct USERINFO
{
int ID;
Char username[10];
Char password[15];
Char mail[10];
}typedef USERINFO;
USERINFO USERINFO;
ZeroMemory (&userinfo,sizeof (UserInfo));
Userinfo.id = 1;
strcat (Userinfo.username, "Wan_dou");
strcat (Userinfo.password, "PASSWORD");
strcat (Userinfo.mail, "wan_dou@citiz.net");
FILE *FP = fopen ("C:/x.txt", "w");
Fwrite (&userinfo,sizeof (UserInfo), 1,FP);
Fclose (FP);
A user's information will be saved. If a variable or array input is less than the input to him automatically fill empty, so it is easier to remove
Fread (&userinfo,sizeof (UserInfo), 1,FP);
But PHP seems to have no struct support, and class with the same operation must be wrong.
So you only have to simulate a structure yourself.
Below is a less mature structure, the most annoying is not to support the variable length of data.

?
Define ("Userinfopath", "Data/userinfo");

Class USERINFO
{
var $UserInfoStruct = Array
(
"UserName" => 10,//Definition structure
"PassWord" => 15,//if int, float,double can refer to the definition in the database or set itself
"Mail" => 50,//50: Length of character required, mail is structure name
);
var $UserName//The variable that really holds the content is here.
var $PassWord;//
var $Mail//, must be the same as the description of the structure definition.

function CheckValue ($STR, $n)//Given a variable, and the specified variable length variable
{
while (strlen ($STR) < $n)//Less fill
{
$str. = ';
}
if (strlen ($STR) > $n)/Rewind
$str = substr ($str, 0, $n);

}
function SaveToFile ($filename)//Save as a file
{
$fp = fopen ($filename, "w");/Open
for (Reset ($this->userinfostruct); $i =key ($this->userinfostruct); next ($this->userinfostruct))
{
$this->checkvalue (& $this-> $i, $this->userinfostruct[$i]);//Call function, check variable provide pointer address
echo $this-> $i;//I put my own debugging, can be deleted.
Fputs ($fp, $this-> $i, $this->userinfostruct[$i])//write $this-> $i is a variable that refers to a phase variable, such as $this->username, and the third parameter can be province, But if you add a more reliable variable that holds the specified length.;

}
Fclose ($FP);//Close
}
function LoadFromFile ($filename)//read from a file
{
$fp = fopen ($filename, "R");/Open
for (Reset ($this->userinfostruct); $i =key ($this->userinfostruct); next ($this->userinfostruct))
{
$this-> $i = fgets ($fp, $this->userinfostruct[$i])//read out characters of the specified length to the variable
echo $this-> $i;//debug yourself
}
Fclose ($FP);//Close
}

};
/*
$UserInfo = new userinfo;//Write
$UserInfo-> UserName = "Wan_dou";
$UserInfo-> PassWord = "1981627";
$UserInfo-> Mail = "wan_dou@sina.com.cn";
$UserInfo-> savetofile ("C:/1.txt");

$UserInfoLoad = new userinfo;//Read
$UserInfoLoad->loadfromfile ("C:/1.txt");
Echo $UserInfoLoad->username;
*/
?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.