我正在尝试读取测试文件并将其写入嵌套的链表,然后将其打印出来。
我正在尝试读取下面的文件,并且如果vlanID
链接列表中没有vlan_tuple
(第二列),则将其添加到列表中,如果存在,则添加嵌套列表vlan_if_list
的接口(第一列)。我的问题是,当我尝试向vlanID
添加新条目时,代码似乎覆盖了vlanID
列表。希望我在这里提供了足够的信息。
要读取的文件
eth1 2
eth2 1
eth3 2
eth4 1
eth5 2
嵌套的链接列表
/* Container for VID Table */
struct vid_addr_tuple
{
char eth_name[ETH_ADDR_LEN]; // Port of Acquisition
char vid_addr[VID_ADDR_LEN]; // VID Address.
time_t last_updated; // last updated time
int port_status; // Port Type i.e. PVID, MTP
int isNew;
struct vid_addr_tuple *next;
uint8_t path_cost; // VID path cost.
struct ether_addr mac;
int membership; // Membership PRIMARY, SECONDARY, TERTIARY
};
主链表
/* Container for VLAN Table */
struct vlan_tuple {
int vlanID;
struct vlan_if_list *vlan_if;
struct vid_addr_tuple *vid;
struct child_pvid_tuple *cpvid;
struct local_bcast_tuple *local;
struct vlan_tuple *next;
};
读取文件的功能
bool read_vlan_if_list(int vlanID) // This vlanID parameter is not used
{
printf("read_vlan_if_list\n");
struct vlan_tuple *temp;
temp = malloc(sizeof(struct vlan_tuple));
temp->next = NULL;
vlan_tuple_head = temp;
vlan_if_head = temp->vlan_if;
// AK - Read VLAN config file
char intName[10]; // Interface name from file
int intVlan; // Interface VLAN from file
int checkVlan = 1;
FILE * pFile; // File pointer
pFile = fopen ("./vlan.conf","r");
if (pFile == NULL)
{
//perror("ERROR: Unable to open vlan.conf\n");
return false ;
} else {
temp->vlanID = 0;
printf("-- vlanID set to 0\n");
while (fscanf (pFile,"%s %d",intName,&intVlan) != EOF)
{
temp = vlan_tuple_head;
printf("-- Start vlanID : %d \n", temp->vlanID);
int flag = 0;
//do
for (temp = vlan_tuple_head; temp->vlanID != 0; temp = temp->next)
{
// Add interface to VLAN if already present
vlanID = temp->vlanID;
printf("vlanID and intVlan : %d %d\n", vlanID, intVlan);
if (intVlan == vlanID)
{
//temp->vlan_if = malloc(sizeof(struct vlan_if_list));
strcpy(temp->vlan_if->if_name,intName);
printf("if %s : %s\n", temp->vlan_if->if_name,intName);
temp->vlan_if->next = malloc(sizeof(struct vlan_if_list));
temp->vlan_if = temp->vlan_if->next;
temp->vlan_if->next = NULL;
}
printf("%d\n",temp->vlanID);
int flag = 1;
printf("flag1 : %d\n", flag);
}
printf("flag2 : %d\n", flag);
if (flag == 0)
{
temp->vlanID = intVlan; //Add new VLAN
printf("New VLAN %d added\n", temp->vlanID);
temp->vlan_if = malloc(sizeof(struct vlan_if_list));
strcpy(temp->vlan_if->if_name,intName);
printf("else If %s added\n", temp->vlan_if->if_name);
}
printf("End vlanID : %d \n", temp->vlanID);
//Point to next vlan_tuple pointer to continue while loop
temp->next = malloc(sizeof(struct vlan_tuple));
temp = temp->next;
temp->vlanID = 0;
temp->next = NULL;
printf("Point to next\n");
}
printf("a\n");
}
printf("b\n");
return true;
}