~~~ Binary File Operations ~~~

Source: Internet
Author: User

There is a binary file of about MB, which stores multiple JPG images. The file format is as follows:

Unrelated data Zone
Jpg image data Zone
Unrelated data Zone
Jpg image data Zone
Unrelated data Zone
Jpg image data Zone
......

The starting mark of the jpg image is "FF D8 FF E0 00 10 4A", and the ending mark is "FF D9 ".

What should I do if I want to read the jpg image data from this file and generate separate image files?

I helped you write a program and tested a file containing only one picture.
Int Len = 0;
Int filesn = 0;

Void readjpg (cfile * SFP, const char * dstfile, unsigned char a, unsigned char B)
{

Cfile NF;
NF. Open (dstfile, cfile: modecreate | cfile: modewrite | cfile: typebinary );
Unsigned T = 0, T1 = 0;
NF. Write (& A, sizeof (char ));
NF. Write (& B, sizeof (char ));
While (1)
{
SFP-> Read (& T, sizeof (char ));
Len ++;
NF. Write (& T, sizeof (char ));
If (t = 0xff)
{
SFP-> Read (& T1, sizeof (char ));
Len ++;
If (T1 = 0xd9)
Break;
Else
NF. Write (& T1, sizeof (char ));

}

}

NF. Close ();
}

Call:

Void test ()
{

Cfile FP;
FP. Open ("86b05621.jpg", cfile: moderead | cfile: typebinary); // modify the file name.
Unsigned char a = 0, B = 0;
Len = 0;

While (1)
{
FP. Read (& A, sizeof (char ));
Len ++;
If (LEN> = FP. getlength ())
Break;

If (A = 0xff)
{

FP. Read (& B, sizeof (char ));
Len ++;
If (B = 0xd8)
{
Filesn ++;
Char filename [256];
Sprintf (filename, "JPG d.jpg", filesn );
Readjpg (& FP, filename, A, B );
}
}

}
FP. Close ();
}

The idea is to first get the FF value and then judge whether the next bit is 0xd8, but here we need to explain, if your other data information contains the FF D8 value, an error will occur. Therefore, we should compare several more bits for the sake of insurance. The JPG header is fixed, the next step is EF E0 00 1E. It is recommended that the landlord compare four more safe ones (just make some changes in the program)

Are you analyzing resource files of a game or program? If so, this resource file should be equipped with an index file! Or in the file itself. The index can be a list index, or a chain table ......
If it is your own file package, it is best to add an index, because this can improve efficiency!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.