c - 使用 c 恢复 .raw 文件

标签 c

我正在尝试读取 .raw 文件并恢复 JPG 文件,然后创建其中的 50 个。我可以编译,但我的输出不显示,尽管我有所有 50 个 jpg 文件。

我已经成功打印了 50 张 jpg 照片,名称从 000.jpg 到 049.jpg。尝试打开它们时,我收到此消息:

解释 JPEG 图像文件时出错(在状态 201 中对 JPEG 库的不正确调用)

我希望在打开另一个文件之前正确地确保文件已关闭

这是我的代码:

#define JPEG1 0xff 0xd8 0xff 0xe0
#define JPEG2 0xff 0xd8 0xff 0xe1
#define BLOCK 512

int main(int argc, char* argv[])
{
// long enough to store the name of a jpeg file
char jpeg_name[4];

// where we are going to store our data
BYTE buffer[512];

// open the picture file  
FILE* file = fopen("card.raw", "r");

// error checking
if (file == NULL)
{
    printf("File could not be opened");
    return 2;
}

// how many jpegs we have at any one time
int jpeg_num = 0;

// check if we're open
int open = 0;

// the outfile we will use for all jpeg files
FILE* jpeg = NULL;

// do this until we can't come up with a full 512, fread returns what it has succesfully      read
// dont need to use address operator for image_data because its an array
while (fread(buffer, sizeof(BYTE), BLOCK, file) == BLOCK)
{
    // this will help us count and name files
    int i = 0;

    // if this the begenning of a jpeg file?
    if ((buffer[0] ==  0xff && buffer[1] == 0xd8 && buffer[2] == 0xff) && (buffer[3] ==   0xe0 || buffer[3] == 0xe1))
    {
        // is there a jpeg file already open?
        //if (fopen("jpeg_name", "r") != NULL)
        if(open == 1)
        {
            fclose(jpeg);
            open = 0;
        }

        // name the jpegs we find
        sprintf(jpeg_name, "%03d.jpg", jpeg_num+i);

        // open the jpeg from sprintf
        jpeg = fopen(jpeg_name, "w");
        open = 1;

        // error checking
        if (jpeg == NULL)
        {
            printf("JPEG file could not be created");
            return 1;
        }    

        // write to our file
        fwrite(buffer, sizeof(BYTE), BLOCK, jpeg);

        // increment counter
        i++;
        jpeg_num += 1;            

    }


}
if(jpeg)
{
  fclose(jpeg);
}
fclose(file);
return 0;

最佳答案

让我们看看。

  • jpeg_name 变量的大小为 3,但您向其中写入了 8 个字节:001.jpg(null)

  • 您正在读取所有内容,但只写入每个 JPEG 样式文件的第一个 block (假设您的标题是正确的)。

  • 您确定二进制字符串 0xff 0xd8 0xff 0xe0 不会出现在 JPEG 的随机二进制文件中吗?

关于c - 使用 c 恢复 .raw 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19580607/

相关文章:

C - 双指针通过函数调用丢失

c - 函数获取单词并将它们放入数组中

c - 为什么在指针赋值后会出现段错误?

c - 交换字符串按值而不是引用传递?

c++ - 在 C 中使用 exe 进行 I/O 重定向

c - 从二进制文件读取结构体 (C)

c - 是否可以添加 getchar();相当于 .o 文件?

c - 内存寻址和堆栈对齐 - 我理解正确吗?

c - 创建线程的pthread_create函数

c++ - 简单的程序无法编译