我正在用C编写CS50x Pset 4 Recover的程序读取第一个块然后尝试将其写入后,收到分段错误

问题描述

CS50X PSet 4恢复

我读取了第一数据块,并检查它是否是jpeg文件的开头。确定jpeg的开头之后,我尝试编写第一个块,这会导致分段错误

我已经隔离了将段前512个字节写入输出文件的分段错误

这是一个相关的代码

// FILE *file = fopen(argv[1],"r");
FILE* file = NULL;
file = malloc(514 * sizeof(char));
file = fopen(argv[1],"r");
printf("count zero = %d\n",count );

// Check if *file is NULL
if (!file)
{
    printf("count return = %d\n",count );
    return 1;
}

// initialize first_image = 1 for first image
// int first_image=0;

// Read block of 512  bytes
count = fread(bytes,sizeof(char),512,file);
printf("count = %d\n",count );

while (count != 0)
{
    printf("count 52 = %d\n",count );

    // check if bytes are the jpg header 0xff 0xd8 0xff and oxe? (jpeg)
    if (*bytes[0] == 0xff && *bytes[1] == 0xd8 && *bytes[2] == 0xff && (*bytes[3] & 0xf0) == 0xe0)
    {
        printf("count 56= %d\n",count );

        if (n == 0)
        {
            // create filename
            sprintf(filename,"%03i.jpg",n );
            filename = malloc(9 * sizeof(char));
            // *filename = malloc(9 * sizeof(char));

            // open first file
            fileout = malloc(512 * sizeof(char));
            fileout = fopen(filename,"w");

            // write data to first file
            fwrite(&bytes,count,

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)