CS50不恢复';不输出任何图像

CS50不恢复';不输出任何图像,c,cs50,C,Cs50,我正在做CS50课程的恢复计划。以下是说明: 在名为recover的目录中名为recover.c的文件中实现您的程序 您的程序应该只接受一个命令行参数,即要从中恢复JPEG的法医图像的名称 如果您的程序不是使用一个命令行参数执行的,它应该提醒用户正确使用,并且main应该 返回1 如果无法打开法医图像进行读取,您的程序应尽可能多地通知用户,main应返回1 如果程序使用malloc,则不得泄漏任何内存 我认为我的代码应该是有效的,但它没有。事实上,它根本不输出任何图像!代码如下:

我正在做CS50课程的恢复计划。以下是说明:

  • 在名为recover的目录中名为recover.c的文件中实现您的程序

  • 您的程序应该只接受一个命令行参数,即要从中恢复JPEG的法医图像的名称

  • 如果您的程序不是使用一个命令行参数执行的,它应该提醒用户正确使用,并且main应该 返回1

  • 如果无法打开法医图像进行读取,您的程序应尽可能多地通知用户,main应返回1

  • 如果程序使用malloc,则不得泄漏任何内存

我认为我的代码应该是有效的,但它没有。事实上,它根本不输出任何图像!代码如下:

#包括
#包括
int main(int argc,char*argv[])
{
FILE*pFile=NULL;
无符号字符*缓冲区=malloc(512);
char*filename=NULL;
int filenumber=0;
//如果用户未打印2项
如果(argc!=2)
{
printf(“用法:./recover image\n”);
返回1;
}
//打开文件
pFile=fopen(argv[1],“r”);
如果(!pFile)
{
fprintf(stderr,“无法打开文件”\n);
返回2;
}
int j=0;
//通过512b块检查卡
//循环(i=0,i++);
while(pFile)
{
int i=0;
i++;
//k=fread(缓冲区,512,i,*文件);
int k=fread(缓冲区,512,i,pFile);
//如果512字节块是jpeg,则生成新的jpeg文件
如果(缓冲区[0]==0xff&&buffer[1]==0xd8&&buffer[2]==0xff&&buffer[3]&0xf0)==0xe0)
{
//如果不是第一个文件,我们应该关闭最后一个
如果(文件名!=NULL)
{
fclose(pFile);
}
//斯普林特
sprintf(文件名为“%03i.jpg”,2);
//FILE=fopen(W)
pFile=fopen(文件名,“w”);
//fwrite(缓冲区,512,j,*file1)
fwrite(缓冲区,512,j,pFile);
//j=j+1
j=j+1;
}

//如果k你有很多问题。在调试器中运行你的代码应该能在一秒钟内发现大部分问题

我们来看看,

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

int main(int argc, char *argv[])
{
    FILE * pFile = NULL;
    unsigned char *buffer = malloc(512);
    char* filename = NULL;  <<==== You never allocate any memory for this. Use an array.
    int filenumber = 0;

    //If user didn't print 2 items
    if(argc != 2)
    {
        printf("Usage: ./recover image\n");
        return 1;
    }

    //Open the file
    pFile = fopen(argv[1], "r");
    if (!pFile)
    {
        fprintf(stderr, "File cannot be opened\n");
        return 2;
    }

    int j=0;
    
    // checking the card by 512b chunks 
    //loop (i=0,  i++);   <<== No information provided by this comment.
    while (pFile)       <<== pFile is your input file. This should never change. ???
    {
        int i =0;
        i++;

        //k=fread (buffer, 512, i, *file);    <<== Useless comment. Nearly same as code below but causes compiler error
        int k = fread(buffer, 512, i, pFile); <<== i is always 1 and must be 1. Don't use variable.
                                              <<== BTW: You should check k **before** using the buffer.


        // if 512 byte block is jpeg, make new jpeg file
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            // if it's not the first file, we should close the last one
            if (filename != NULL)
            {
                fclose(pFile);  <<== Yikes!!! This is your input file.
            }

            //sprintf  <<== Yes, that's obvious. Useless comment.
            sprintf(filename, "%03i.jpg", 2);  <<== Yikes!! You never allocate memory. NULL pointer!!
                                               <<== Why do you always print 2? you have a counter.

            //FILE = fopen (W)  <<== Again no useful information in comment
            pFile = fopen(filename, "w");  <<== Feed NULL into fopen and kill pFile.

            // fwrite (buffer, 512, j, *file1)  <<== you know what I mean...
            fwrite (buffer, 512, j, pFile);  <<== You only have 1 buffer, why write j blocks?

            //j=j+1  <<== obvious
            j = j + 1;
        }

        // if k<512 - end of the loop
        if (k < 512)   <<== fread returns number of elements, i.e. 1, not number of bytes.
        {
            << you return without
               - closing files
               - freeing buffer
            return 0;
        }
   <<== Now you go back to top of the loop and want to read next block from your raw file but pFile was killed in the loop.

    }
    free(buffer);
}
#包括
#包括
int main(int argc,char*argv[])
{
FILE*pFile=NULL;
无符号字符*缓冲区=malloc(512);

char*filename=NULL;您遇到了大量问题。在调试器中运行代码应该能在一秒钟内发现大部分问题

我们来看看,

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

int main(int argc, char *argv[])
{
    FILE * pFile = NULL;
    unsigned char *buffer = malloc(512);
    char* filename = NULL;  <<==== You never allocate any memory for this. Use an array.
    int filenumber = 0;

    //If user didn't print 2 items
    if(argc != 2)
    {
        printf("Usage: ./recover image\n");
        return 1;
    }

    //Open the file
    pFile = fopen(argv[1], "r");
    if (!pFile)
    {
        fprintf(stderr, "File cannot be opened\n");
        return 2;
    }

    int j=0;
    
    // checking the card by 512b chunks 
    //loop (i=0,  i++);   <<== No information provided by this comment.
    while (pFile)       <<== pFile is your input file. This should never change. ???
    {
        int i =0;
        i++;

        //k=fread (buffer, 512, i, *file);    <<== Useless comment. Nearly same as code below but causes compiler error
        int k = fread(buffer, 512, i, pFile); <<== i is always 1 and must be 1. Don't use variable.
                                              <<== BTW: You should check k **before** using the buffer.


        // if 512 byte block is jpeg, make new jpeg file
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            // if it's not the first file, we should close the last one
            if (filename != NULL)
            {
                fclose(pFile);  <<== Yikes!!! This is your input file.
            }

            //sprintf  <<== Yes, that's obvious. Useless comment.
            sprintf(filename, "%03i.jpg", 2);  <<== Yikes!! You never allocate memory. NULL pointer!!
                                               <<== Why do you always print 2? you have a counter.

            //FILE = fopen (W)  <<== Again no useful information in comment
            pFile = fopen(filename, "w");  <<== Feed NULL into fopen and kill pFile.

            // fwrite (buffer, 512, j, *file1)  <<== you know what I mean...
            fwrite (buffer, 512, j, pFile);  <<== You only have 1 buffer, why write j blocks?

            //j=j+1  <<== obvious
            j = j + 1;
        }

        // if k<512 - end of the loop
        if (k < 512)   <<== fread returns number of elements, i.e. 1, not number of bytes.
        {
            << you return without
               - closing files
               - freeing buffer
            return 0;
        }
   <<== Now you go back to top of the loop and want to read next block from your raw file but pFile was killed in the loop.

    }
    free(buffer);
}
#包括
#包括
int main(int argc,char*argv[])
{
FILE*pFile=NULL;
无符号字符*缓冲区=malloc(512);

char*filename=NULL;我重复上一个问题中的注释:正确的缩进很重要。不要让我们读起来困难,更重要的是,你自己也读起来困难!你的代码的后半部分根本没有逻辑块的缩进。如果你必须搜索对应的方括号,因为它们都在第一列,那么它就是不容易阅读。您必须第一眼看到逻辑块。如果您的目的是让人们不阅读您的代码,那就是正确的方法。
//循环(i=0,i++);while(pFile)
注释与下一行中的指令无关,但仅与下面几行相关。这比帮助更让人困惑。什么是
pFile
?您的输入文件在运行时会更改吗?不是注释。拒绝应用缩进会分散人们的注意力。特别是在被告知几次之后。我重复了您的p前一个问题:正确的缩进很重要。不要让我们读起来困难,更重要的是,你自己也读起来困难!在代码的后半部分中,你根本没有逻辑块的缩进。如果你必须搜索相应的括号,因为它们都在第一列,那就不容易读。你必须看到f处的逻辑块第一眼。如果你的目的是让人们远离阅读你的代码,那就是最好的方法。
//循环(i=0,i++);而(pFile)
注释与下一行中的指令无关,但只与下面几行相关。这比帮助更让人困惑。什么是
pFile
?您的输入文件在运行时会发生更改吗?不是注释。拒绝应用缩进会分散人们的注意力。特别是在被告知几次之后。我不理解这部分:'pFile=fopen(文件名,“w”);如果您从顶部开始阅读,您会发现您从未为
文件名
分配任何内存,当您将其放入
fopen
时,它仍然是
NULL
。为什么您认为,如果只有512字节的缓冲区,您可以一次写入512字节的
j
块?为什么大小会增加(你总是在每个块之后执行
j=j+1
)你说的“为文件名分配任何内存”是什么意思?我应该怎么做?很抱歉这么说。但是你应该重新开始,看看你的关于指针的教科书。你必须使用
malloc
来获取一些指针可以指向的内存。或者你可以简单地使用数组
char filename[10];
来代替指针。我不理解这部分:'pFile=fopen(filename,“w”);如果您从顶部开始读取,您会发现您从未为
文件名
分配过任何内存,并且当您将其放入
fopen
时,它仍然是
NULL
。为什么您认为,如果只有512字节的缓冲区,您可以一次写入512字节的
j
块?为什么大小会增加(你总是在每个块后执行
j=j+1
)你说的“为文件名分配内存”是什么意思?我应该怎么做?很抱歉,你应该重新开始,看看关于指针的教科书。你必须使用
malloc
来获得指针可以指向的内存。或者你可以简单地使用ar