Solved

values to Structure

Posted on 2009-07-15
5
205 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:kishan66
  • 3
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 24863139
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 24863142
>> What you have to do, is change this :

Same for the ip of course.
0
 
LVL 14

Expert Comment

by:flob9
ID: 24863217
I aggree with infinity.

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

wich does the same thing than malloc & strcpy
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24863291
If your platform supports strdup, then that's indeed an alternative, although less portable.
0
 
LVL 14

Expert Comment

by:flob9
ID: 24863418
Yes strdup, sorry for the typo.

And you're right for the portability :)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now