为结构数组问题赋值?

时间:2011-03-28 13:30:41

标签: c arrays

我编写了一个程序来读取测试文件并在C程序中填充结构值。

所有其他事情都没问题。

但是,当我尝试填充界面值结构时,它会显示最近的strtok()值。

测试文件看起来像,

[text file]

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


[/textfile]

我的代码是,

/*
 * configparse1.c
 *
 *  Created on: Mar 24, 2011
 *      Author: nco_user
 */
/*
 * parsetest1.c
 *
 *  Created on: Mar 24, 2011
 *      Author: sjayaram
 */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>


void stcpy(char *str1, char *str2);

/* Common information structure. */
typedef struct CINFO {
    int num_nodes;
    int num_aps;

}cinfo;

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

/* 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;


/* File pointer .*/
FILE *f;


int main()
{
    int j,k;
    char string[1000], seps[] = " \n,( )-";
    char *p;
    char s[10];
    int i=0;

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


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

    memset(&zero_ifintf,0,sizeof(zero_ifintf));
    while(fgets( string, sizeof(string)-1, f) != NULL)
    {
        /* Break into tokens. */
        p = string;
        p = strtok( string, seps ); /* Find first token*/
        //printf("Beginning tok=%s\n",p);
        memset(nodeuse.name,0,sizeof(nodeuse.name));
        memset(nodeuse.val,0,sizeof(nodeuse.val));
        while( p != NULL )
        {
            printf("Token: %s\n", p);

            //printf("Hello-1 \n");
            /* Getting node and APs value. */
            if(strcmp(p,"NODES")==0)
            {
                int a;
                a = atoi (strtok( NULL, seps )); /* Find next token*/
                commontoolinfo.num_nodes=a;
                //printf("Hello-2 \n");
                //commontoolinfo.num_aps=0;
                //printf("Hello-1 \n");
            }

            if(strcmp(p,"APS")==0)
            {
                int num_aps;
                num_aps=(atoi)(strtok(NULL,seps));
                commontoolinfo.num_aps=num_aps;
                //printf("Hello-3 \n");
            }

            if(strcmp(p,"TYPE")==0)
            {
                p = strtok( NULL, seps ); /* Find next token*/
                printf("type name= %s \n",p);
                strcpy(s,p);
                //printf("S= %s \n",s);
                //printf("Hello-4 \n");
            }

            if (strcmp(p,"NODE")==0)
            {
                int a;
                //p1= (char *)malloc(2*sizeof(char*));
                a = atoi (strtok( NULL, seps )); /* Find next token*/
                //int i;
                //printf("a=%d \n",a);

                switch (a)
                {
                case 1:
                {
                    //printf("Hello-5 \n");
                    //i=0;
                    //printf("hello 5-1\n");
                    p = strtok( NULL, seps ); /* Find next token*/
                    //printf("hello 5-2\n");
                    if(strcmp(p,"NUM_IF")==0)
                    {
                        //printf("hello 5-21\n");
                        int a;
                        a = (atoi)(strtok( NULL, seps ));
                        //printf("hello 5-22\n");
                        node1[i].num_if=a;
                        printf("************* NUM_OF_INTF=%d**********/n/n",node1[i].num_if);
                    }
                    else if (strcmp(p,"VM_ID")==0)
                    {
                        int a;//=malloc(sizeof(2*int));
                        node1[i].vm_id=0;
                        a = (atoi) (strtok( NULL, seps )); /* Find next token*/
                        //memset(node1[i].vm_id,0,sizeof(node1[i].vm_id));
                        node1[i].vm_id=a;
                        printf("VM-ID=%d \n",node1[i].vm_id);
                        //printf("Hello-7 \n");
                    }
                    else if(strcmp(p,"MEM_SIZE")==0)
                    {

                        //long int a = malloc(sizeof(long int));
                        int a;
                        a = (atoi) (strtok(NULL,seps));
                        node1[i].memsize=0;
                        //memset(node1[i].memsize,0,sizeof(node1[i].memsize));
                        node1[i].memsize = a;
                        printf("mem-size=%ld\n",node1[i].memsize);
                        //free(a);
                        //printf("Hello-8 \n");
                    }
                    else if(strcmp(p, "OS")==0)
                    {
                        printf("Hello-9 \n");
                        //char *s;
                        char *s;// = (char*)malloc(2*sizeof(char*));
                        //node1[i].OS = malloc(sizeof(char*));
                        s = strtok(NULL,seps);
                        printf("********* S = %s **********\n\n",s);
                        //memset((char*)node1[i].OS,0,sizeof(node1[i].OS));
                        //node1[i].OS =s;
                        //printf("test1 ----\n");
                        strcpy((node1[i].OS),s);
                        //free(s);

                        printf("OS type:%s\n",(node1[i].OS));

                        printf("Hello-10 \n");
                    }
                    else 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++;
                }


                //printf("Hello-11 \n");
                break;

                case 2:
                {
                    i=1;

#if 0
                    p = strtok( NULL, seps ); /* Find next token*/
                    memset(node1[i].name,0,sizeof(p));
                    //printf("test11 \n");
                    //memset(node1[i].OS,0,sizeof(p));
                    //printf("test12 \n");
                    strcpy(node1[i].name,p);
                    //printf("test4 \n");
                    printf("node1[i]->name= %s \n",node1[i].name);
                    //printf("test5 \n");
                    //printf("Hello-12 \n");
                    //printf("2.OS type:%s\n",node1[0].OS);
                    break;
#endif
                    //printf("hello 5-1\n");
                    p = strtok( NULL, seps ); /* Find next token*/
                    //printf("hello 5-2\n");
                    if(strcmp(p,"NUM_IF")==0)
                    {
                        //printf("hello 5-21\n");
                        int a;
                        a = (atoi)(strtok( NULL, seps ));
                        //printf("hello 5-22\n");
                        node1[i].num_if=a;
                        printf("************* NUM_OF_INTF=%d**********/n/n",node1[i].num_if);
                    }
                    else if (strcmp(p,"VM_ID")==0)
                    {
                        int a;//=malloc(sizeof(2*int));
                        node1[i].vm_id=0;
                        a = (atoi) (strtok( NULL, seps )); /* Find next token*/
                        //memset(node1[i].vm_id,0,sizeof(node1[i].vm_id));
                        node1[i].vm_id=a;
                        printf("VM-ID=%d \n",node1[i].vm_id);
                        //printf("Hello-7 \n");
                    }
                    else if(strcmp(p,"MEM_SIZE")==0)
                    {
                        //long int a = malloc(sizeof(long int));
                        int a;
                        a = (atoi) (strtok(NULL,seps));
                        node1[i].memsize=0;
                        //memset(node1[i].memsize,0,sizeof(node1[i].memsize));
                        node1[i].memsize = a;
                        printf("mem-size=%ld\n",node1[i].memsize);
                        //free(a);
                        //printf("Hello-8 \n");
                    }
                    else if(strcmp(p, "OS")==0)
                    {
                        printf("Hello-9 \n");
                        //char *s;
                        char *s;// = (char*)malloc(2*sizeof(char*));
                        //node1[i].OS = malloc(sizeof(char*));
                        s = strtok(NULL,seps);
                        printf("********* S = %s **********\n\n",s);
                        //memset((char*)node1[i].OS,0,sizeof(node1[i].OS));
                        //node1[i].OS =s;
                        //printf("test1 ----\n");
                        strcpy(node1[i].OS,s);
                        //free(s);

                        printf("OS type:%s\n",node1[i].OS);

                        printf("Hello-10 \n");
                    }
                    else if(strcmp(p,"IP_ADDR_ETH")==0)
                    {

                        printf("--------------hi1 \n");
                        int comp,a;
                        printf("------------hi2 \n");
                        a = atoi (strtok( NULL, seps )); /* Find next token*/
                        printf("A is value=%d \n\n ",a);
                        if(a < (node1[i].num_if))
                        {
                            printf("-------------hi3-0 \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("-------------hi3-1 \n");
                            printf("Node[%d] interface-eth[%d]address=%s \n\n", i,a,(node1[i].node_if[a].ip_addr) );
                        }

                        printf("---------------------hi4 \n");
                        printf("*************8 Node[%d] interface-eth[%d]address=%s \n\n", i,0,(node1[i].node_if[0].ip_addr) );
                        printf("*****************8 Node[%d] interface-eth[%d]address=%s \n\n", i,1,(node1[i].node_if[1].ip_addr) );
                        printf("*****************8 Node[%d] interface-eth[%d]address=%s \n\n", i,2,(node1[i].node_if[2].ip_addr) );
                    }
                    i++;
                }
                break;

                default:
                    break;
                    //printf("Hello-13 \n");
                }



#if 0
                    if(strcmp(p,"1")==0)
                    {
                        node1 node1[20];
                        i=0;
                        p = strtok( NULL, seps ); /* Find next token*/
                        memset(node1[i].name,0,sizeof(p));
                        strcpy(node1[i].name,p);
                    }
#endif
                    //free(p1);
                    //printf("VM NAME: %s \n",nodeuse.name);
                    //printf("test1 ********** \n");
                    //printf("Hello-15 \n");
            }

            //printf("Hello-16 \n");

            p = strtok( NULL, seps ); /* Find next token*/
            //printf("Hello-2 \n");
        }  //end of inner while
        //printf("Hello-17 \n");
    }   //end of first while



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



    printf("Hello-18 \n");
    for(j=0;j<=2;j++)
    {
        //printf("test11 ********** \n");
        printf("VM ip: %s \n",node1[j].name);
    }


    //printf("test2 ********** \n");
    for(k=0;k<commontoolinfo.num_nodes;k++)
    {
        int comp;
        printf("NODE%d number of interfaces=%d \n",(k+1),node1[k].num_if);

        for(comp=0;comp < node1[k].num_if;comp++)
        {
            printf("Node[%d] of Interface ETH-[%d] IP ADDR=%s \n\n",(k+1),comp,node1[k].node_if[comp].ip_addr);

        }
        printf("NODE%d VM_ID=%d\n",(k+1),node1[k].vm_id);
        printf("NODE%d MEM_SIZE=%ld\n",(k+1),node1[k].memsize);
        printf("NODE%d OS=%s\n",(k+1),node1[k].OS);
    }

    fclose(f); /* Close opening file pointer. */

    printf("End of program \n");
    return 0;

}


void stcpy(char *to, char *from)
{
     while(*from)
          *to++ = *from++;
      *to = '\0'; /* null terminates the string */
}

我的问题是什么?

1 个答案:

答案 0 :(得分:0)

commontoolinfo.num_nodes=a; 
use strcpy(commontoolinfo.num_nodes,a)

...因为strtok会返回char * value ...