c中返回语句之前的分段错误

时间:2014-08-14 10:55:50

标签: c mpeg2-ts

我成功编译并运行了代码。但是在我得到分段问题之后,我一次又一次地检查了代码,但我没有得到分段错误的原因,有人请帮我解决这个问题。

这是我的代码..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[])
{
        unsigned long long int I_frame=0,P_frame=0,B_frame=0;
        printf("File name is :%s\n",argv[1]);
        int read=0,ts_header_size=0,adapt=-1,pld=-1,k=0;
        char buff[4],picture_buff[2];
        FILE *fp=NULL,*fp1=NULL,*fout=NULL;
        fp=fopen(argv[1],"r");
        if(fp==NULL)
        {
                printf("failed to  open the file :%s\n",argv[1]);
                exit(0);
        }
        fp1=fopen(argv[2],"r");
        if(fp1==NULL)
        {
                printf("failed to open file :%s\n",argv[2]);
                exit(0);
        }
        printf("second file name is:%s\n",argv[2]);
        fout=fopen(argv[3],"w+");
        if(fout==NULL)
        {
                printf("failed to open file :%s\n",argv[3]);
                exit(0);
        }
        printf("output file name is :%s\n",argv[3]);
        read=fread(buff,1,4,fp);
        while(read)
        {
                ts_header_size=4;
                if((buff[0]==0x00)&&(buff[1]==0x00)&&(buff[2]==0x01)&&(buff[3]==0x00))//picture header checking if present count I,B,P frames
                {
                        fwrite(buff,1,4,fout);                             
                        fread(picture_buff,1,2,fp);
                        fwrite(picture_buff,1,2,fout);
                        k=(((picture_buff[1])&0x38)>>3);                          
                        if(k==1)
                                I_frame++;
                        if(k==2)
                                P_frame++;
                        if(k==3)
                                B_frame++;

                        read=fread(buff,1,188,fp);
                }

                fwrite(buff,1,1,fout);
                buff[0]=buff[1];
                buff[1]=buff[2];
                buff[2]=buff[3];
                read=fread(buff+3,1,1,fp);

        }
         /*printing the I,P,B frame count*/
        printf("no of I frames are :%lld\n",I_frame);
        printf("no of P frames are :%lld\n",P_frame);
        printf("no of B frames are :%lld\n",B_frame);
        printf("hi\n");
        return 0;

}    

1 个答案:

答案 0 :(得分:3)

下面:

 read=fread(buff,1,188,fp);

您正在尝试将188个字节读入只有4字节的数组。

另一个问题是你在这里抛弃了之前的return值:

 read=fread(buff+3,1,1,fp);