Like Tree1Likes

Reading File Headers

This is a discussion on Reading File Headers within the C Programming forums, part of the General Programming Boards category; Hi friends, Can anyone tell me how to read JPEG files header using C ??...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    24

    Reading File Headers

    Hi friends,
    Can anyone tell me how to read JPEG files header using C ??

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Hello, you may want to start by telling what OS/compiler you are using and then it would be possible to suggest a graphics library for you to use to load the jpeg.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    24
    Hello Andrew,
    I am actually trying to develop an backup application which backsup all the jpeg files in a backup folder.

    For that I need to read the header of JPEG files.

    I am using Windows-7 OS and MS Visual C++ compiler.

    so can u please suggest me something so that I can get a good start ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Basically, you just want to identify files in JPEG format through the file header? If so, maybe you could just code it yourself.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    24
    can u give me a short example of how to do it ?

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Here is the JPEG "header" format. Notice the marker that designates the file as a JPEG. Basically you would look for that marker and based on whether or not it was there would decide if it is or isn't a JPEG.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    24
    well I already have tha header format !!! look m very new to this kind of programming ..I have worked with files using C ..but that is just to an extent of reading and writing files..not much.... so can u please explain me with example !!!!!

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Here is the file FAQ we have here. Read the binary files section. You are just reading in the first value of the file as designated by the file format you apparently already have.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by parimal.s.dave View Post
    Hello Andrew,
    I am actually trying to develop an backup application which backsup all the jpeg files in a backup folder.

    For that I need to read the header of JPEG files.

    I am using Windows-7 OS and MS Visual C++ compiler.

    so can u please suggest me something so that I can get a good start ?
    Why wouldn't you just use the file extension?

    Windows 7 does a horrible thing... it hides file extensions from the user.
    Open your control panel -> Folder Settings -> View and uncheck "Hide extensions for known file types".

    Now you can see that all your jpegs are actually named with a .jpg extension.
    For example: FamilyPicnic turns out to be FamilyPicnic.jpg

    There's no need to open the files, just copy everything with the .jpg extension.
    Last edited by CommonTater; 07-11-2011 at 07:28 AM.

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by parimal.s.dave View Post
    well I already have tha header format !!! look m very new to this kind of programming ..I have worked with files using C ..but that is just to an extent of reading and writing files..not much.... so can u please explain me with example !!!!!
    Well, if you know how to read from files, and you know what the header should look like then I don't really see the problem here. If you still are having problems then I suggest you ask a more specific question.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by parimal.s.dave View Post
    well I already have tha header format !!! look m very new to this kind of programming ..I have worked with files using C ..but that is just to an extent of reading and writing files..not much.... so can u please explain me with example !!!!!
    Tell you what... get started on the project, work it out as far as you can. If you get stuck (really stuck, not lazy stuck) post your code and I'm sure we can give you some suggestions.

    In the mean time it's very easy to copy files by extension on Windows systems...

    FindFirstFile Function (Windows) ... to enumerate the files
    CopyFile Function (Windows) ... to do the actual copying

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    24
    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 !!!!!

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by CommonTater View Post
    There's no need to open the files, just copy everything with the .jpg extension.
    Though it'd be a problem if the file's been labeled with a jpeg extension by mistake.
    But reading the first 2 bytes of a file is a conclusive way of determining jpeg'ness.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by parimal.s.dave View Post
    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 !!!!!
    You have all the relevant information - so give it a go and get back in case you're stuck.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by parimal.s.dave View Post
    I want to make the program absolutely portable !!!!!!! so theres where I am stuck !!!!!
    There are no portable ways of dealing with the filesystem, if you need to (eg) work recursively through a directory tree. So to maximize your portability, keep this section of the code completely separate from everything else. Eg, you have a set of functions that reads the filesystem and accumulates an array of file paths to check.

    This can be handed off to the jpg checking code, which can be 100% portable. Just stick to standard C: fopen() or open(), fread() or read(), etc.

    Quote Originally Posted by itCbitC View Post
    But reading the first 2 bytes of a file is a conclusive way of determining jpeg'ness.
    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.
    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

Page 1 of 2 12 LastLast
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, 02: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, 01:07 AM
  5. file headers
    By sleex in forum C++ Programming
    Replies: 3
    Last Post: 02-17-2002, 12:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21