• C

values to Structure

Hi,

how can i pass values to a structure at runtime....from a file"A" with two lines in it. Using C.

lets suppose..........my senario:-
FILE  "A"--
name=webserver
serverIP=1.1.1.1;
---------------------
typedef struct
{ char *name;
 char * serverIPaddress;
}Server1;
typedef Server1 myServer;
i have to construct "myServer" structure using values from file.

using fgets i am getting the values and had even split them..but when i am trying to assign the value(S) to structure.....some thing is going wrong...both items in structure point to same memory..according my code. there fore is overwritten always.

how to avoid this.??



lets suppose  
while(fgets(filestr,50,FileSource)!=NULL) {
				OK = FillServerInfo(filestr); //where filestr is char*
			}
loop 1:
FillServerInfo(char *str)   //lets suppose str contains string "name=server" in it
  {
   char *strtemp
  // i split the value in str and only send the data through strtemp
    strtemp = strtok(filestr,"=");
   a = getnum(strtemp);  // returns the numeric value of strtemp. name=1,serverIp=2;
   strtemp = strtok(NULL,"=");
 
 switch(a)
 {
  case 1:
      myServer->name = strtemp;
      return 1;
  case 2:
   myServerIp->ServerIpaddress=strtemp;
  return  1;
}
 
will get correct value in myServer->name......but in next loop it is updated to serverIpaddress values.

Open in new window

kishan66Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
The problem is that you use the same filestr buffer to read each line, and then the strtemp returned by strtok will always point within that buffer, even if the buffer changes.

What you have to do, is change this :

>>       myServer->name = strtemp;

to something like :

        myServer->name = (char*) malloc(strlen(strtemp) + 1);
        strcpy(myServer->name, strtemp);

in order to copy the data, rather than copy the pointer.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Infinity08Commented:
>> What you have to do, is change this :

Same for the ip of course.
0
flob9Commented:
I aggree with infinity.

I can just add : myServer->name = strup(strtemp);

wich does the same thing than malloc & strcpy
0
Infinity08Commented:
If your platform supports strdup, then that's indeed an alternative, although less portable.
0
flob9Commented:
Yes strdup, sorry for the typo.

And you're right for the portability :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.