使用 fread 时出现分段错误,无法从文件中读取数据

发布于 2024-11-06 15:28:21 字数 1659 浏览 0 评论 0原文

这里想要从文件 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情未る 2024-11-13 15:28:21

这将不起作用,因为您尝试读取二进制文件,而您的文件是文本。

其次 - 您需要在尝试打开文件后检查 fp 是否为 NULL - 只是一个建议。

第三 - 即使文件是二进制文件,这也不起作用,

fread(&keys, sizeof (struct classifier), 3, fp);

因为

//                                      vvv
fread(&keys, sizeof (struct classifier), 1, fp);

keys 不是数组,您需要只读取块。

This will not work, because you try to read binary file, while your file is text.

Second - you need to check if fp is NULL after trying to open the file - just an advice.

Third - even if the file was binary, this wouldn't work, as

fread(&keys, sizeof (struct classifier), 3, fp);

should be

//                                      vvv
fread(&keys, sizeof (struct classifier), 1, fp);

As keys is not an array and you need to read just on block.

暮色兮凉城 2024-11-13 15:28:21

您似乎没有为您的 3 把钥匙分配足够的空间。

void addrule(struct classifier keys)
{
....
    fread(&keys, sizeof (struct classifier), 3, fp);    // Here you read 3 keys and put into &keys, but you gave only one struct to your method

也许有一个循环读取每个键:

void addrule(struct classifier keys)
{
....
    while fread(&keys, sizeof (struct classifier), 1, fp)
    {
        insert(key,keys);
    }
    fclose(fp);
}

You seem to not allocate enough room for your 3 keys.

void addrule(struct classifier keys)
{
....
    fread(&keys, sizeof (struct classifier), 3, fp);    // Here you read 3 keys and put into &keys, but you gave only one struct to your method

Maybe with a loop reading one key at each :

void addrule(struct classifier keys)
{
....
    while fread(&keys, sizeof (struct classifier), 1, fp)
    {
        insert(key,keys);
    }
    fclose(fp);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文