0

我开始学习 C 并且我绝对不擅长它,我正在学习 CS50 课程,第 4 周(内存),我必须查看.raw.jpeg

当我运行我的代码时,图像会按顺序完美地创建,等等等等,但是当我打开它们时,它们是空白的(一个有图像的某些部分,但不是我想要实现的),

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

void makeJPEG(int nof, unsigned char chunk[512]);

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("Just give 1 argument, not less, not more.\n");
        return 1;
    }
    printf("El nombre del archivo es -> %s\n", argv[1]);
    FILE *file = fopen(argv[1], "r");
    if (file == NULL)
    {
        return 1;
    }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    unsigned char chunk[512];
    int nof = 0;
    while (fread(chunk, sizeof(char), 512, file) == 512)
    {
        if (sizeof(chunk) == (sizeof(char) * 512))
        {
            if (chunk[0] == 0xff && chunk[1] == 0xd8 && chunk[2] == 0xff)
            {
                printf("\n****************************************************\n");
                printf("\n\nProbably a JPEG\n");
                if ((chunk[3] & 0xf0) == 0xe0)
                {
                    printf("Definitely a JPEG\n");
                    char filenameMain[8];
                    makeJPEG(nof, chunk[512]);
                    sprintf(filenameMain, "%03i.jpg", nof);
                    printf("The file -> %s <- was created successfully\n\n", filenameMain);
                    printf("\n****************************************************\n");
                    nof++;
                }
            }
            else
            {
                printf("Not a JPEG\n");
            }
        }
    }
    fclose(file);
    printf("\n*Final*\n");
    return 0;
}

void makeJPEG(int nof, unsigned char chunk[512])
{
    FILE* img = NULL;
    //Declaring a String of 8 bytes
    char filename[8];

    //Giving that String the name of the file
    sprintf(filename, "%03i.jpg", nof);

    //Writing the new jpg file
    img = fopen(filename, "a");
    fwrite(chunk, sizeof(char), 512, img);

    //Closing the image and freeing the memory
    fclose(img);
}

我认为我的问题在于我使用“块”变量的方式以及如何将其发送到“makeJPEG”,但我不知道我做错了什么,我再说一遍,我完全是个新手。

PS:对不起我的英语错误,并提前感谢您的帮助:)

4

1 回答 1

1

makeJPEG(nof, chunk[512]); is wrong. That attempts to pass element 512 of chunk. Change it to makeJPEG(nof, chunk);. That passes the buffer chunk (by pointer). Your compiler should have given you a warning message for this. If it did not, turn on warning messages and pay attention to them.

The main loop of your program continues reading from the file into chunk and checking each one to see if it is a JPEG. That is wrong. Only the first part of a file will have the indicators that it is a JPEG. To copy the entire file, you should check only the first part and then copy all of it.

Additionally, the loop increments nof, and makeJPEG opens a new file each time it is called. This would write every chunk of an input file to many different output files, which is not generally useful. If you want your program to copy one JPEG file, then redesign it so that chunks are written to the same file each time, not to a new file. If you want your program to process many input files, you need to redesign it in other ways.

This is useless: sizeof(chunk) == (sizeof(char) * 512). The size of chunk is always the size it was defined to be, 512 bytes.

This needs changing: fread(chunk, sizeof(char), 512, file) == 512. You need to use result = fread(chunk, sizeof chunk, 1, file), where result is previously declared with type size_t. Then you need to check result. If it is 512 bytes, you have a complete chunk and should write it to the output file and loop to process more chunks. If it is zero, no more data could be read, and you are done with this file (aside from special circumstances we can neglect for now). If it is between zero and 512, you got a partial chunk, which is likely the end of the file. You should write it to the output file, after which you are done with this input file (barring special circumstances).

于 2020-03-17T14:38:16.087 回答