?
Solved

values to Structure

Posted on 2009-07-15
5
Medium Priority
?
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 opening and writing to files 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.
Suggested Courses

762 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