2

我在读取 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);
}
4

4 回答 4

0

fopen()出错时返回 NULL 指针,而不是 -1。如果 fopen 失败,您继续使用 NULL 指针执行文件操作。

这是错误的:

fp=fopen("test2","rb");
if(fp==-1)
{
    printf("unsucessful");
    exit(0);
}

应该是(并注意我对空格的使用)

fp = fopen("test2", "rb");
if (fp == NULL) {
    printf("Could not open test2\n"); /* A meaningful error message */
    exit(0);
}
于 2013-09-12T08:58:10.070 回答
0

您的代码中几乎没有错误。

1.你需要检查fp==NULL而不是失败fp==-1fopen()返回NULL

2.在for循环中你使用逗号,我猜是AND。

for(k=0,l=addr_offset_in_inarray;l<sizeof(rawdata),k<1024;k++,l++)  
                                                  ^ 
for(k=0,l=addr_offset_in_inarray;((l<sizeof(rawdata) )&& (k<1024));k++,l++)    
                                                      ^^  

3.没有声明变量i。

需要删除警告
您没有使用很多变量,即使它们
在打印时使用值类型不匹配进行初始化。

修改后的代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


void main()

{

int count,i;

FILE *fp;

long file_size;

unsigned char rawdata[1132];

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

if(fp==NULL)

{

 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,(unsigned int) ftell(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: %ld", sizeof(payload));

    size=sizeof(payload);


data_extraction(size, payloadint,buffer_depth,buffer_width,length_of_fft);

printf("\n s:%ld",file_size);

printf("\n ft:%x",(unsigned int)ftell(fp));


printf("\n****----end----****");

fclose(fp);

}
于 2013-09-12T09:15:04.973 回答
0

如果你在一个类 Unix 平台上,你可以用原始系统调用替换fopen, fread,来看看会发生什么。fseek

#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int fd = open("test2", O_RDONLY);
if (fd < 0) {
    printf("Could not open test2\n");
    exit(0);
}

然后使用read()代替fread()lseek()代替fseek()等。

于 2013-09-12T13:25:45.730 回答
0

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

于 2013-09-12T17:55:51.483 回答