c - fread 中的段错误

标签 c segmentation-fault fread

我在读取 1500 个或更多以太网数据包时遇到 fread 中的段错误。这里“test2”是二进制文件,文件大小为 22.6MB。 1132是每个数据包中有用数据点的数量,142点带有头信息,因此被跳过。

这是主程序:

void main()
{
    int count;
    FILE *fp;
    long file_size;
    unsigned char rawdata[1132];

    fp=fopen("test2","rb");

    if(fp==-1)
    {
        printf("unsucessful");
        exit(0);
    }

    long int before=ftell(fp);

    fseek(fp,0,SEEK_END);
    file_size=ftell(fp);
    rewind(fp);

    long int after=ftell(fp);

    //skip first 142 bytes(header information)since its not required
    fseek(fp,142,SEEK_SET);

    long int s=ftell(fp);
    int length_of_fft=4096;
    int buffer_width=128;
    int buffer_depth=1024;
    int k,aa,payloadindex=0,l=0,j,a;
    int no_of_data_pts_to_be_read=1132;
    int no_of_ethernet_pkts_to_be_read=1500;
    int q=no_of_ethernet_pkts_to_be_read*buffer_depth;
    unsigned char payload[q];
    unsigned int payloadint[q];
    int no_of_data_pks_read=0;
    int reading_for_first_time=1;
    unsigned char data_from_file[no_of_ethernet_pkts_to_be_read][buffer_depth];
    int addr_offset_in_inarray=0;
    int udp_counter_values[no_of_ethernet_pkts_to_be_read];
    unsigned int rawdataint[1132];
    long int size;

    count=0;

    for(a=0; a<no_of_ethernet_pkts_to_be_read; a++)
    {
        int p=fread(rawdata,1 ,sizeof(rawdata), fp);

        count=p;

        //----------- to check if all data points have been read, i,e the pointer must be at a position wich is a multiple of 1132 which is[(1274-142=1132)*(a+1)],( since 142 bytes were skipped in the beginning )
        printf("\n\n %d\t  Start=%x\t\t  Stop=%x\t   Count=%d\t   Address=%x",no_of_data_pks_read, rawdata[0], rawdata[sizeof(rawdata)-1],count,
        ftello(fp));

        if(count==no_of_data_pts_to_be_read)
        {
            printf("\nNumber of data points read in packet %d (of %d) is %d ",no_of_data_pks_read, no_of_ethernet_pkts_to_be_read, count);
            reading_for_first_time=0;

            //--------------converting char array rawdata into int array and then call udp
            for(i=0;i<1132;i++)
                rawdataint[i]=rawdata[i]-'\0';

            udp_counter_values[a]=check_UDPpacketCount(&addr_offset_in_inarray, &rawdataint,10,no_of_data_pks_read,1132);
            //   printf("\n--------udp:: %d ",udp_counter_values[a]);

            //-----------------create new mat and compute payload and put the contents of array rawwdata into the respective row of the new matrix
            int k,t,w,time=0;

            for(k=0,l=addr_offset_in_inarray;l<sizeof(rawdata),k<1024;k++,l++)
            {
                data_from_file[no_of_data_pks_read][k]=rawdata[l];      

                //   printf("\n datafile:%d",data_from_file[no_of_data_pks_read][k]);
            }

            for(t=0;t<1024;t++)
            {
                payload[payloadindex++]=data_from_file[no_of_data_pks_read][t];
            }

            no_of_data_pks_read++;  
        }
        else
        {
            count=0;
            printf("\n not equal, exiting ");
            exit(0);
        }
    }

    //------convert payload to int array and send to data extraction function
    for(i=0;i<sizeof(payload);i++)
    {
        payloadint[i]=payload[i]-'\0';
    }
    printf(" sizepayload: %d", sizeof(payload));
    size=sizeof(payload);
    data_extraction(size, payloadint,buffer_depth,buffer_width,length_of_fft);
    printf("\n s:%d",file_size);
    printf("\n ft:%x",ftell(fp));
    printf("\n****----end----****");
    fclose(fp);
}

最佳答案

正如前面提到的,您可能会用完所有堆栈,请尝试将所有静态分配的变量设为全局变量或使用动态分配。这应该会改善您的情况。

关于c - fread 中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18759636/

相关文章:

无法使用 getppid() 获取预期的 id

c - 从同一进程发送和接收UDP数据包

r - 快速组合和转置许多固定格式的数据集文件

C:fread返回0

c++ - 具有返回条件的openmp优化

c++ - 如何在C中按顺序对带有数字和字母的文件名进行排序?

c++列表迭代器的段错误

C : Cannot access memory error, 递归查找 AVL 树高度时

c - strtok 的段错误

C 指针、结构和 fwrite