我们必须一次读取512字节的“ .raw”文件并将其存储在缓冲区中。如果我们碰到了卡中缓冲区数组的前四个字节正确的一点,那么我们将创建一个文件并继续写入512字节的块,直到我们碰到另一组指示新JPEG文件的四个字节为止(然后关闭前一个文件,打开一个新文件,然后写入文件)
解决此问题的其他人已实例化一个char数组来存储文件名(001.jpg,002.jpg ... 049.jpg)。并将数组长度列为[10]。这真让我感到困惑,因为.raw文件包含49个jpg图像。
一旦读取了文件末尾,while循环就会终止,但是不幸的是我的代码无法正确读取最后一个文件(即使我放了while(!eof(file)){..}。 / p>
// remember filenames
char *infile = argv[1];
// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL) {
fprintf(stderr, "Could not open %s.\n", infile);
return 1;
}
FILE *outptr = NULL;
char outfilename[8]; // stores all the JPEGs into separate files
unsigned char buffer[512]; //assigns a buffer 512 elements long
//the jpg file names are incremented 001.jpg, 002.jpg, etc. using i
int i = 0;
while (!feof(inptr)) {
//reads 512 elements, each one byte long from the input file card.raw, and stores it in the buffer array
fread(buffer, 1, 512, inptr);
//If the first four elements of the buffer array match these values, then we have a new jpg
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0) {
jpg = true;
if (outptr != NULL) { //there's an open file
//close the previous file
fclose(outptr);
}
sprintf(outfilename, "%03i.jpg", i);
outptr = fopen(outfilename, "w");
i++;
// fwrite(buffer, 1, 512, outptr);
}
if (jpg == true) { //then we have a jpg file open that we need to write to
fwrite(buffer, 1, 512, outptr);
}
}
fclose(outptr);