c - segmentation fault while using fread and not able to read the data from the file -
here want copy data file enter_data , pass function insert(key,keys) segmentation fault , more on not able read data file
here classifier structure, , packet_filter has structure of ip , udp in want enter src , dst ip adddress , src , dst port number
struct classifier { int key_node; struct packet_filter pktfltr; struct classifier *next; }__attribute__((packed)); void addrule(struct classifier keys) { int key; file *fp; fp = fopen("enter_data","r"); fread(&keys, sizeof (struct classifier), 3, fp); insert(key,keys); fclose(fp); }
file: enter_data key = 822; keys.key_node = 822; inet_aton("172.28.6.137", &(keys.pktfltr.ip.ip_src)); inet_aton("172.28.6.10",&(keys.pktfltr.ip.ip_dst)); keys.pktfltr.protocol.proto.uh_sport = ntohs(1032); keys.pktfltr.protocol.proto.uh_dport = ntohs(5000); keys.next = null; key = 522 ; keys.key_node = 522; inet_aton("172.28.6.87", &(keys.pktfltr.ip.ip_src)); inet_aton("172.28.6.110",&(keys.pktfltr.ip.ip_dst)); keys.pktfltr.protocol.proto.uh_sport = ntohs(1032); keys.pktfltr.protocol.proto.uh_dport = ntohs(5010); keys.next = null; key = 522 ; keys.key_node = 522; inet_aton("172.28.6.87", &(keys.pktfltr.ip.ip_src)); inet_aton("172.28.6.110",&(keys.pktfltr.ip.ip_dst)); keys.pktfltr.protocol.proto.uh_sport = ntohs(1032); keys.pktfltr.protocol.proto.uh_dport = ntohs(5011); keys.next = null;
this not work, because try read binary file, while file text.
second - need check if fp
null
after trying open file - advice.
third - if file binary, wouldn't work,
fread(&keys, sizeof (struct classifier), 3, fp);
should
// vvv fread(&keys, sizeof (struct classifier), 1, fp);
as keys
not array , need read on block.
Comments
Post a Comment