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.
- are you looking for the extension or the type of your file ?Cédric Julien– Cédric Julien2012-10-15 09:05:53 +00:00Commented Oct 15, 2012 at 9:05
- if file is not stored with extension then how can you get it?Deepak– Deepak2012-10-15 09:06:20 +00:00Commented Oct 15, 2012 at 9:06
- 3How 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.paddy– paddy2012-10-15 09:06:23 +00:00Commented Oct 15, 2012 at 9:06
- Ah Sorry, i mean the type of the file like whether its a pdf, doc, odt , mp3, jpeg etcParth Shah– Parth Shah2012-10-15 09:07:03 +00:00Commented Oct 15, 2012 at 9:07
- @paddy: Is there any other way? No C library function?Parth Shah– Parth Shah2012-10-15 09:08:46 +00:00Commented Oct 15, 2012 at 9:08
| Show 6 more comments
2 Answers
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.
1 Comment
Parth Shah
Yes I think this will help. How can I do that? Can you guide me?
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