0

Is there a C library function to get the extension of file? I know that I can design a function on my own to get extension after '.' but not all files are stored with their extensions when we read them.

11
  • are you looking for the extension or the type of your file ? Commented Oct 15, 2012 at 9:05
  • if file is not stored with extension then how can you get it? Commented Oct 15, 2012 at 9:06
  • 3
    How do you expect to get the extension if it's not stored with the filename? Are you trying to determine file formats such as PNG etc? If that's what you want to do and can't rely on extension, you'll have to read the file headers. Commented Oct 15, 2012 at 9:06
  • Ah Sorry, i mean the type of the file like whether its a pdf, doc, odt , mp3, jpeg etc Commented Oct 15, 2012 at 9:07
  • @paddy: Is there any other way? No C library function? Commented Oct 15, 2012 at 9:08

2 Answers 2

4

So you'd like to get the type of a file? Maybe the command 'file' in Linux is what you want. You can check its source code.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I think this will help. How can I do that? Can you guide me?
3

The file command in Linux uses a library called libmagic (don't confuse with libmagick) to check the 'magic' bytes in the file itself to determine the likely content type.

The library is fairly cross platform, it's well documented, for example here: http://linux.die.net/man/3/libmagic

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.