Here is my source code,the problem is the string cannot hold the value after it left the function.Can any one give me some advise?
Code:
#include"tool.h"
void OpenAccountFile(FILE *uDB,int *index,int *AccNo);
void GetUserInfo(string Name,string ID,string* ptStr,int *slen);
string db = "userinfo.txt";
string pdb = "password.txt";
int main(void)
{
FILE *userDB = NULL;
string* ptStr = NULL;
string Name = NULL , ID = NULL;
//string Password = NULL;
int index , AccNo , slen;
srand(time(NULL));
OpenAccountFile(userDB,&index,&AccNo);
GetUserInfo(Name,ID,ptStr,&slen);
//Variable Name and ID cannot pass out after function GetUserInfo was called
puts(Name); <-----Error occured here
puts(ID); <------And here
return 0;
}
void OpenAccountFile(FILE *uDB,int *index,int *AccNo)
{
string tmpName = NULL , tmpId = NULL;
if((uDB = fopen(db,"r")) == NULL)
{
uDB = fopen(db,"a");
*index = 1;
*AccNo = 1;
}
else
{
uDB = fopen(db,"r");
while((fscanf(uDB,"%d %d %s %s",index,AccNo,tmpName,tmpId)) != EOF)
{
++*index;
++*AccNo;
}
}
fclose(uDB);
return;
}
void GetUserInfo(string Name,string ID,string* ptStr,int *slen)
{
char buf[20];
ptStr = malloc(sizeof(string));
Name = ptStr;
printf("Please enter your apply name : ");
scanf("%19[^\n]",buf);
fflush(stdin);
ptStr = malloc(strlen(buf) + 1);
strcpy(Name,buf);
*slen = strlen(Name);
puts(Name);
ptStr = malloc(sizeof(string));
ID = ptStr;
printf("Please enter your ID card number : ");
GetIDCardNo(buf);
fflush(stdin);
ptStr = malloc(strlen(buf) + 1);
strcpy(ID,buf);
puts(ID);
return;
}