Thread: Reading File Headers

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by parimal.s.dave View Post
    Hi commonTater,
    well I know I can do it with file extension !!!! but still the program would require system calls to be used ..... I want to make the program absolutely portable !!!!!!! so theres where I am stuck !!!!!
    No it would not require system() callls... The FindFirstFile(), FindNextFile(), CopyFile() etc calls that you would use are part of the standard Windows API and can be called directly from C code and will work on all versions of Windows since Win95.

    The portability issue is going to be problematic since each OS has it's own filesystem and thus it's own API calls for locating and enumerating files.

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MK27 View Post
    It's a conclusive way of determining if a file is not a jpg; there is no conclusive way to say that it is a jpg. You have to make an educated guess.
    Not having used the file header method before, this is interesting... Would you say this is more or less reliable than using file extensions?

    For a certainty it will be slower, since each file has to be opened, verified and closed before copying.

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CommonTater View Post
    Not having used the file header method before, this is interesting... Would you say this is more or less reliable than using file extensions?
    It will be a better check for the "is not", because presuming this is what image viewers, etc, do, it is a conclusive test -- a corrupted jpeg file without a proper ffd8 at the beginning will be rejected by the software.

    But there could be binary files that coincidentally start with ffd8. Depending on what this is for, I guess first doing a case insensitive check for jpg or jpeg, then checking the header to make sure it really is such a thing (or can be treated as such by some software) would be thorough.

    For a certainty it will be slower, since each file has to be opened, verified and closed before copying.
    For sure, because the time constraint will be disk I/O, and presumably you have to read the directory info either way.

    If you are happy to presume the files in the filesystem are properly labelled, you might as well just use the suffix. I guess if that proves problematic you could go the header route.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #19
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MK27 View Post
    It's a conclusive way of determining if a file is not a jpg; there is no conclusive way to say that it is a jpg. You have to make an educated guess.
    IBTD, as per this site the first 2 bytes of any jpeg file should == 0xffd8

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by itCbitC
    IBTD, as per this site the first 2 bytes of any jpeg file should == 0xffd8
    The format for itCbitC files is such that the first two bytes is 0xffd8. As such, when you come across a file for which the first two bytes is 0xffd8, that alone cannot confirm that the file format is JPEG, because it might be an itCbitC file.

    But if you come across a file for which the first two bytes is not 0xffd8, you can be certain that it is neither in JPEG nor itCbitC format.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    The format for itCbitC files is such that the first two bytes is 0xffd8. As such, when you come across a file for which the first two bytes is 0xffd8, that alone cannot confirm that the file format is JPEG, because it might be an itCbitC file.

    But if you come across a file for which the first two bytes is not 0xffd8, you can be certain that it is neither in JPEG nor itCbitC format.
    lol!

    Alright I stand corrected, because as per this the jpeg magic no. is the first 4 bytes i.e. 0xffd8ffe0.

  7. #22
    Registered User
    Join Date
    Jul 2011
    Posts
    24
    thank u everyone for your help !!!! i have done my job!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading the data bitween the different headers in a buffer.
    By ariseramesh8 in forum C Programming
    Replies: 1
    Last Post: 04-27-2008, 01:40 AM
  2. Unexpected end of file, splitting headers into .h/.cpp
    By Shamino in forum C++ Programming
    Replies: 19
    Last Post: 12-11-2007, 01:08 PM
  3. reading packet headers
    By iain in forum Linux Programming
    Replies: 2
    Last Post: 12-02-2004, 08:25 PM
  4. Including C++ headers (and functions) to a C-file
    By torbjorn in forum C Programming
    Replies: 4
    Last Post: 10-24-2002, 12:07 AM
  5. file headers
    By sleex in forum C++ Programming
    Replies: 3
    Last Post: 02-17-2002, 12:52 PM