Thread: string.......HARD!!Can any one help?tks

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Angry string.......HARD!!Can any one help?tks

    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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you want to return an updated pointer, you need to do it like this

    Code:
    void foo ( char **p ) {
      *p = malloc( 10 );
      strcpy( *p, "hello" );
    }
    int main ( void ) {
      char *str = NULL;
      foo ( &str );  // note, you now have a ptr to a ptr
      printf( "ptr is %p, string is %s\n", str, str );
      free( str );
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM