使用c语言恢复.raw文件
我正在尝试读取一个.raw文件并恢复JPG文件,然后创建50个。我可以编译,但我的输出不显示,尽管我有全部50个jpg文件 我已经成功打印了50张jpg照片,照片名称从000.jpg到049.jpg。尝试打开它们时,我收到以下消息: 解释JPEG图像文件时出错(在状态201下对JPEG库的调用不正确) 我希望在打开另一个文件之前正确地确保文件已关闭 这是我的密码:使用c语言恢复.raw文件,c,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 0xe
#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样式文件的第一个块(假设您的头是正确的)
- 您确定二进制字符串0xff 0xd8 0xff 0xe0不会出现在JPEG中的随机二进制文件中吗
- 我有相同的编程任务。我在代码中注意到的一点是,jpeg的第四个字节的范围从0xe0到0xef。在你的代码中我只看到
缓冲区[3]==0xe0 | |缓冲区[3]==0xe1欢迎来到StackOverflow!谢谢你发布你的代码,但是请在你的问题中多加一点描述:你有什么问题,你期望的结果是什么,到目前为止?仔细阅读这篇文章将有助于我们更好地回答你的问题。谢谢
jpeg\u name
只有4个字符,但是您的sprintf()
将尝试写入至少8个字符,例如001.jpg
加上空字节。好的,我已经更改了char-jpeg\u name[3];要更改jpeg_名称[8],但我不知道如何更正我的fwrite语句。非常感谢。