Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i wanted to know the file name even after renaming.

for example i changed .jpeg to .txt then how can find that extension using header.
Posted
Updated 2-May-13 19:35pm
v2

You are looking for something like the file command on Linux with it's magic data file.

If you only want to identify a few image file types, just read some bytes from the begin of the file and check them for image file specific sequences:

C++
const char *lpszType = NULL;
char pBuffer[16] = "";
FILE *f = fopen(lpszFile, "rb");
if (f)
{
    fread(pBuffer, 1, 16, f);
    if (0 == memcmp(pBuffer, "BM", 2) && 0 == memcmp(pBuffer + 6, "\0\0\0", 4))
        lpszType = "BMP";
    else if (0 == strcmp(pBuffer, "II*") || 0 == strcmp(pBuffer, "MM*"))
        lpszType = "TIFF";
    else if (0 == memcmp(pBuffer, "\x89PNG\r\n\x1A\n", 8))
        lpszType = "PNG";
    else if (0 == memcmp(pBuffer, "GIF87a", 6) || 
        0 == memcmp(pBuffer, "GIF89a", 6))
        lpszType = "GIF";
    else if (0 == memcmp(pBuffer, "\xFF\xD8\xFF", 3) && 
        pBuffer[3] >= 0xE0 && pBuffer[3] <= 0xEF &&
        4 == strlen(pBuffer + 6))
        lpszType = "JPEG";
    fclose(f);
}
 
Share this answer
 
Comments
P Uday kishore 3-May-13 6:07am    
can i have brief explanation on this please????
Jochen Arndt 3-May-13 6:24am    
This will open the file which name is specified by lpszFile and read 16 bytes into pBuffer.
Then the content of the buffer is then checked for signatures indicating specific image file types. E.g. bitmap files begin with the upper case letters 'B' and 'M' and have four NULL bytes at offset 6. JPEG files begin with the hex bytes FF, D8, and FF, followed by a byte in the range E0 to EF. At offset 6 of JPEG files is 4 character wide null terminated string (this indicates the type; e.g. 'EXIF').

To know the format of image files, have a look at the Wikipedia entering the image type.

If you don't know the used functions (fopen, fread, fclose, memcmp, strcmp) so far, look them up in the MSDN. They are all standard C library functions.
P Uday kishore 6-May-13 7:03am    
thanks for the explanation and code.I have one more doubt here is there any change in binary format in the new versions of jpeg/gif/png etc....
Jochen Arndt 6-May-13 7:15am    
It depends. Some formats have a versions field which may be used to indicate how the file must be parsed. But for the basic checks from my code there should be no changings in the future (otherwise, it would be a new format).

JPEG is a little bit special because it covers multiple types. The code from my example is mainly for JFIF data. But with all JPEG formats, there is a 4 character wide null terminated string at offset 6 indicating the type (e.g. "JFIF", "JFXX", "EXIF"). When checking only for the first two bytes (0xFF, 0xD8), all future formats should be also catched.
P Uday kishore 10-May-13 8:28am    
Hi,i got a new work can u help me in this context??/

how to get a file type if it is given without extension as input.
do we have any criteria to find that???
It sounds as if you mean that you want to be able to find out what the content of a file is, regardless of filename and extension. In order to do so you would need to research various file format specifications.

Wrt JPEGs, all JPEGs always begin with hexadecimal FF D8 (ASCII ÿØ) - the rest is wrapper specific. You can search for the specification yourself. However, I did find this handy page with a lot of different file format identifiers (though I cannot guarantee the accuracy) including a set of various JPEG format headers: http://www.garykessler.net/library/file_sigs.html[^]

Regards,
Ian.
 
Share this answer
 
It can't be done. The only way to do this kind of thing is to record the filename before it is renamed and look at the cached value.
 
Share this answer
 
Basically you cant because,Even you read all files as binary but , for reading binary you need file format of that file and but obeouse reseon you dont have it.
Second is that you can make log of file like before name change you stores all file names in some log file or variables and cross verify after name changed.
As H.Brydon says ,you may check chached value but you can get only reccent entries not all updates.
 
Share this answer
 
Comments
P Uday kishore 3-May-13 2:32am    
idea behind this is in my application i have to find the pornographic image in a given set of images.
if any one changed the extension of that image even then i have to do it for that i need know how to read file in binary mode.
Coder Block 3-May-13 2:36am    
yup then you even make it more simple do you know the path of file where that image resided???
P Uday kishore 3-May-13 2:52am    
ya we wil hav the path.
i hav to check the extension and then i hav to send it to the scanning..so can i have any way to get that information.
Coder Block 3-May-13 3:00am    
Best way is that,look you have binary data before renaming file and after renaming file so just compare both and if compare match then you will get the file that you want.Look
Maybe following manner,
//Before rename of the file..
//Read file data in binary format and store it in CByteArray.
//now user will change the file extention or name and not content of the file so,
//Scan all files in same path match data that you have with each and every file data and if match found then work it!!!
P Uday kishore 3-May-13 3:03am    
but we cant get that info in investigation.that's y we have to our self find the file type and send it to scanning.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900