jpeg - 从原始数据中恢复 Jpeg 文件
问题描述
我需要从存储卡(原始数据)中恢复 jpeg 文件。我已经完成了下面的代码,但是我遇到了一个无法识别来源的段错误。总结一下,我做了一个循环来读取 512 字节的块并寻找特定的 jpeg 标头。如果它是第一个 jpeg,程序将打开一个文件并继续写入。如果 jpeg 不是第一个,关闭前一个图像并继续写入它。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
// Check usage
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
// Open file
FILE *file = fopen(argv[1], "r");
if (!file)
{
fprintf(stderr, "Could not open %s.\n", argv[1]);
return 1;
}
// open array to store the chunks with enough memory
unsigned char buffer [512];
// variables jpeg count
int img_count = 0;
// open filename img to write to
char filename[8];
FILE *img = NULL;
// create loop to read 512 chunks
while (fread(buffer, 512, 1, file) > 0)
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
// if theres already a jpeg open
if (img_count > 0)
{
fclose(img);
sprintf(filename, "%i03.jpg", img_count);
img = fopen(filename, "w");
img_count++;
} // first jpeg img count == 0
else if (img_count == 0)
{
sprintf(filename, "%i03.jpg", img_count);
img = fopen(filename, "w");
img_count++;
}
}
//if this is not a new jpeg header, just write to img
if (img_count > 0)
{
fwrite(buffer, 512, 1, img);
}
else
{
continue;
}
}
fclose(img);
fclose(file);
return 0;
}
解决方案
找到它...错字我有“%i03.jpg”而不是 %03i.jpg”
推荐阅读
- python - 设置运行的python程序的最大内存使用量
- c++ - 如何更精确地打印浮点数?
- go - Golang segmentio/kafka-go 消费者不工作
- java - 无法根据以下锚标记定位元素
- excel - Excel Multiple Criteria Serach 和如果找到插入“x”
- angular - 在 Angular 6 中使用 Mathquill 接口
- python - 将数据与比赛得分的理想值进行比较
- spring - spring cloud feign调用上传文件的方法抛出异常参数不存在
- java - 从性能增强的角度进行代码审查
- javascript - 无法将道具中的功能传递给组件 | 反应