简体   繁体   English

给结构数组赋值问题!

[英]assigning values to struct array problem!

The following code is shorten form, I have two structurs named struct INTERFACE and struct node. 以下代码是简化形式,我有两个名为struct INTERFACE和struct node的结构体。 For my purpose I have 2 nodes and first node have two interfaces and second node have three interfaces. 出于我的目的,我有2个节点,第一个节点有两个接口,第二个节点有三个接口。

I have one config file from wher I need to read the information and store it in the above structures. 我需要读取一个信息并将其存储在上述结构中,因此只有一个配置文件。

So in my main program while loop , I read the config file using strtok() fun, and store respectively. 因此,在主程序while循环中,我使用strtok()fun读取了配置文件,并分别进行存储。 In the if loop (if(strcmp((p,"IP_ADDR_ETH")==0), I compared the IP address token and retrive the ip address and store in the interfaces of each node. Why i am using switch case because if NODE-1 means first node, NODE-2 means second node. Here I shown only for first node. 在if循环(if(strcmp((p,“ IP_ADDR_ETH”)== 0))中,我比较了IP地址令牌并检索了IP地址并存储在每个节点的接口中。为什么我使用交换器,因为如果NODE -1表示第一个节点,NODE-2表示第二个节点,此处仅显示第一个节点。

In the if loop I retrive the IP address of first node and store it in node structure's interface value. 在if循环中,我检索第一个节点的IP地址并将其存储在节点结构的接口值中。 by, 通过,

(node1[i].node_if[a].ip_addr)= p;
                                //strcpy((node1[i].node_if[a].ip_addr),p);
                        printf("Node[%d] interface-eth[%d] address=%s \n\n", i,a,(node1[i].node_if[a].ip_addr) );

Inside the while loop, for each node ,I can access the interface structure and get ipaddr value properly. 在while循环内,对于每个节点,我可以访问接口结构并正确获取ipaddr值。 But out side the while loop, if I am printing the each nodes interface values, I am getting only the second node last interface value, I am not able to get each nodes interface ipaddress properly. 但是在while循环之外,如果我打印每个节点的接口值,则仅获得第二个节点的最后一个接口值,则无法正确获取每个节点的接口ipaddress。

My part of the main code is as follows, sorry if the code length is more again, but I gave only nessary portion only, 我的主要代码如下,抱歉,如果代码长度又变长了,但是我只给出了必要的部分,

/* Node information structure. */
typedef struct node {
    char name[20];
    char val[20];
    char OS[10];
    int vm_id;
    int num_if;
    //char *OS;
    if_val1 node_if[10];
    long int memsize;
}node1;

/* Interfaces definition structure. */
typedef struct INTERFACE {
    char *ip_addr;
    char *netmask;
    char *gateway;
} if_val1,if_val2;

I have the above two structure.


/* File pointer .*/
FILE *f;


int main()
{

    node1 nodeuse;
    node1 node1[20];
    //if_val1 if_val[10];   
    if_val1 zero_ifintf;

    //int a;
    f=fopen("test.config","r");
    if(!f)
    return 1;

    while(fgets( string, sizeof(string)-1, f) != NULL)
    {
        /* Break into tokens. */
        p = string;
        p = strtok( string, seps ); /* Find first token*/
        while( p != NULL )
        {
        if (strcmp(p,"NODE")==0)
            {

            a = atoi (strtok( NULL, seps )); /* Find next token*/
            switch (a)
                {
                case 1:
                {
                int i=0;
                if(strcmp(p,"IP_ADDR_ETH")==0)
                        {

                            printf("--------------hi1 \n");
                            int comp,a;
                            printf("------------hi2 \n");
                            a = atoi (strtok( NULL, seps )); /* Find next token*/

                            if(a < (node1[i].num_if))
                            {
                                printf("-------------hi3 \n");
                                p = strtok( NULL, seps ); /* Find next token*/
                                (node1[i].node_if[a].ip_addr)= p;
                                //strcpy((node1[i].node_if[a].ip_addr),p);
                        printf("Node[%d] interface-eth[%d] address=%s \n\n", i,a,(node1[i].node_if[a].ip_addr) );
                            }

                            printf("---------------------hi4 \n");
                        }
                    i++;
                }
                }}  //end of while loop

        //outside while loop printf 's  

            printf("NODE[%d] INTERFACE-ETH-[%d] ADDRESS: %s \n",1,0,(node1[0].node_if[0].ip_addr));
            printf("NODE[%d] INTERFACE-ETH-[%d] ADDRESS: %s \n",1,1,(node1[0].node_if[1].ip_addr));
            printf("NODE[%d] INTERFACE-ETH-[%d] ADDRESS: %s \n",2,0,(node1[1].node_if[0].ip_addr));
            printf("NODE[%d] INTERFACE-ETH-[%d] ADDRESS: %s \n",2,1,(node1[1].node_if[1].ip_addr));
            printf("NODE[%d] INTERFACE-ETH-[%d] ADDRESS: %s \n",2,2,(node1[1].node_if[2].ip_addr));

why the structure values not printed as expected. 为什么结构值未按预期打印。

also, I am trying to assign tokenized string value to other string using strcpy() function. 另外,我正在尝试使用strcpy()函数将标记化字符串值分配给其他字符串。 But when compile segmentation fault came. 但是,当编译分段错误出现时。 Is it possible to declare char *string and assing value using strcpy()?. 是否可以使用strcpy()声明char * string和assing值?

my config file as follows, [test.config] 我的配置文件如下,[test.config]

TYPE VM vm1
TYPE PM pm1 
NODES 2
APS 2
TYPE wired
NODE-1 NUM_IF 2
NODE-1 IP_ADDR_ETH-0 10.114.12.1
NODE-1 IP_ADDR_ETH-1 10.114.12.2
NODE-1 VM_ID 1
NODE-1 MEM_SIZE 512
NODE-1 OS FEDORA

NODE-2 NUM_IF 3
NODE-2 IP_ADDR_ETH-0 10.114.14.1
NODE-2 IP_ADDR_ETH-1 10.114.14.2
NODE-2 IP_ADDR_ETH-2 10.114.14.3
NODE-2 VM_ID 2
NODE-2 MEM_SIZE 1GB
NODE-2 OS CENTOS
NODE-2 10.114.12.7

[/test.config] [/test.config]

Thank you, 谢谢,

Arun 阿伦

strcmp(p,"IP_ADDR_ETH")

will never match because at this time p points to "NODE". 永远不会匹配,因为此时p指向“节点”。 Once you fixed that, it should work. 修复该问题后,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM