使用 fread 时出现分段错误,无法从文件中读取数据
这里想要从文件 Enter_data 复制数据并将其传递给函数 insert(key,keys) 但我遇到分段错误,而且我无法从文件中读取数据,
这里分类器是一个结构,而 packet_filter具有 ip 和 udp 的结构,因为我想输入 src 和 dst ip 地址以及 src 和 dst 端口号
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;
Here want to copy the data from the file enter_data and pass it to the function insert(key,keys) but i get a segmentation fault and more over i am not able to read the data from the file
here classifier is a structure, and packet_filter has structure of ip and udp in that i want to enter src and dst ip adddress and src and 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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将不起作用,因为您尝试读取二进制文件,而您的文件是文本。
其次 - 您需要在尝试打开文件后检查
fp
是否为NULL
- 只是一个建议。第三 - 即使文件是二进制文件,这也不起作用,
因为
keys
不是数组,您需要只读取块。This will not work, because you try to read binary file, while your file is text.
Second - you need to check if
fp
isNULL
after trying to open the file - just an advice.Third - even if the file was binary, this wouldn't work, as
should be
As
keys
is not an array and you need to read just on block.您似乎没有为您的 3 把钥匙分配足够的空间。
也许有一个循环读取每个键:
You seem to not allocate enough room for your 3 keys.
Maybe with a loop reading one key at each :