LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-06-2008, 08:51 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
C++: Is there a way to determine the file type of a given file?


I'm trying to find out what type of file I'm dealing with. I can't look at the extension because this particular file extension can be ascii, binary, gzipped ascii, or gzipped binary.

Is there a way to determine the file type without doing something like

system("file some_file");

?

Thanks
 
Old 03-06-2008, 09:36 PM   #2
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Open the file and look inside. If is starts with "\x1f\x8b\x08\x08", it's a gzip file. Otherwise, count byte frequencies. Text files will consist primarily of spaces and letters, and are unlikely to contain any non-whitespace control characters.

Edit: This assumes you merely want to know whether a file is text or binary. If you want to know that kind of text or binary file it is, follow the other suggestions.

Last edited by Dan04; 03-08-2008 at 01:09 AM.
 
Old 03-06-2008, 09:45 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by Dan04 View Post
Open the file and look inside. If is starts with "\x1f\x8b\x08\x08", it's a gzip file. Otherwise, count byte frequencies. Text files will consist primarily of spaces and letters, and are unlikely to contain any non-whitespace control characters.
Good tip, thanks.
 
Old 03-06-2008, 10:37 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
A better way might be to use [man] libmagic, which is what the desktop systems and file both use.
ta0kira

PS I hear it even makes "magic cookies."

Last edited by ta0kira; 03-06-2008 at 10:41 PM.
 
Old 03-07-2008, 11:46 AM   #5
nullmind
LQ Newbie
 
Registered: Mar 2008
Posts: 9

Rep: Reputation: 0
You could use a mime type library or command.
 
Old 03-07-2008, 06:14 PM   #6
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by ta0kira View Post
A better way might be to use [man] libmagic, which is what the desktop systems and file both use.
ta0kira

PS I hear it even makes "magic cookies."
Not to sound too neophyte-ish, but ever used this before? I'm having a heck of a time finding docs and/or examples. Best I've put together is this:

Code:
#include <stdio.h>
#include <magic.h>

int main() {
  magic_t myt = magic_open(MAGIC_CONTINUE|MAGIC_ERROR|MAGIC_DEBUG|MAGIC_MIME);
  printf("magic output: '%s'\n",magic_file(myt,"/some/file"));

  return 0;
}
but it always returns:

magic output: '(null)'

confused.
 
Old 03-07-2008, 07:38 PM   #7
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by BrianK View Post
Not to sound too neophyte-ish, but ever used this before? I'm having a heck of a time finding docs and/or examples.

...blah blah blah...
I found the source for "file" & saw that I was missing a line & using "magic_open" wrong. Here's a working example:



Code:
#include <stdio.h>
#include <magic.h>

int main() {
  struct magic_set *magic = magic_open(MAGIC_MIME|MAGIC_CHECK);
  magic_load(magic,NULL); // loads the default mime type definition
  printf("magic output: '%s'\n",magic_file(magic,"/some/file"));

  return 0;
}
 
Old 03-07-2008, 08:43 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
No I haven't, but a careful look at the manpage and some deductive hacking of the file source code led me to this:
Code:
#include <stdio.h>
#include <magic.h>

int main() {
  magic_t myt = magic_open(MAGIC_CONTINUE|MAGIC_ERROR/*|MAGIC_DEBUG*/|MAGIC_MIME);
  magic_load(myt,NULL);
  printf("magic output: '%s'\n",magic_file(myt,__FILE__));
  magic_close(myt);

  return 0;
}
Getting rid of the debug flag will save you a lot of garbage output.
ta0kira

Edit: I don't know how I missed your post above this one. I think I loaded this thread several hours ago and just now got to it. Sorry!

Last edited by ta0kira; 03-07-2008 at 08:48 PM.
 
Old 06-04-2009, 07:13 AM   #9
gubacsek
LQ Newbie
 
Registered: Jun 2009
Posts: 7

Rep: Reputation: 0
Hello Everyone!

I'm trying to determine the filetype (bin or text) of a file and finally found this thread with your code:

Quote:
Originally Posted by ta0kira View Post
No I haven't, but a careful look at the manpage and some deductive hacking of the file source code led me to this:
Code:
#include <stdio.h>
#include <magic.h>

int main() {
  magic_t myt = magic_open(MAGIC_CONTINUE|MAGIC_ERROR/*|MAGIC_DEBUG*/|MAGIC_MIME);
  magic_load(myt,NULL);
  printf("magic output: '%s'\n",magic_file(myt,__FILE__));
  magic_close(myt);

  return 0;
}
BUT! when i'm trying to compile it, i'm getting these errors:
Code:
helloworld.c:(.text+0xe04): undefined reference to `magic_open'
helloworld.c:(.text+0xe1a): undefined reference to `magic_load'
helloworld.c:(.text+0xe4f): undefined reference to `magic_file'
helloworld.c:(.text+0xeab): undefined reference to `magic_close'
collect2: ld returned 1 exit status
Since i didn't found any valuable information about magic.h, and how to use it, i'm asking You to help me, please!

What can be missing? If i don't include magic.h, MAGIC_CONTINUE|MAGIC_ERROR and so on aren't declared also. So something happens when i unclude it, but the functions open, load, file, and close are not there.

I'm on a gentoo linux 2.6.29-r5.

Thanks in advance!
 
Old 06-04-2009, 11:45 AM   #10
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
You have to link against libmagic.
 
Old 06-05-2009, 03:06 AM   #11
gubacsek
LQ Newbie
 
Registered: Jun 2009
Posts: 7

Rep: Reputation: 0
Question

Quote:
Originally Posted by jiml8 View Post
You have to link against libmagic.

I'm sorry jiml8, i'm a (not 100%, but) noob with C++, i just started learning it on my own.
So i have to link against libmagic? How can i do it?
I tryed to include libmagic, but there is no such file.
(only libmgagic.so, la)

I would appreciate you help!
 
Old 06-05-2009, 05:14 AM   #12
gubacsek
LQ Newbie
 
Registered: Jun 2009
Posts: 7

Rep: Reputation: 0
Nevermind! I FOUND IT \o/ !!!

Code:
g++ /usr/lib/libmagic.so helloworld.c -o helloworld
Thank you guys for pointing me to the right direction! I learned a lot today
 
Old 06-05-2009, 06:51 AM   #13
gitanshu
LQ Newbie
 
Registered: May 2009
Posts: 18

Rep: Reputation: 1
You can use file command on that file.and grep for different results.
 
Old 06-05-2009, 08:42 AM   #14
gubacsek
LQ Newbie
 
Registered: Jun 2009
Posts: 7

Rep: Reputation: 0
You are right gitanshu, this was my first approach. But using libmagic, i learned some new stuff, which is my goal now
Anyway, thank you for your reply!
 
Old 06-05-2009, 09:15 AM   #15
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Quote:
g++ /usr/lib/libmagic.so helloworld.c -o helloworld
I am a bit surprised that worked. Proper syntax is this:

g++ -lmagic helloworld.c -o helloworld

The system expects a shard library to start with lib and end with .so therefore you do not have to put the full library name in. Further, by doing it this way, the system will link with the latest version of the library that is on the system automatically.

Last edited by jiml8; 06-05-2009 at 09:17 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how file browser detects file type without .3 sufix? Leon_Amirreza Fedora 2 03-18-2007 11:20 PM
howto convert .flw (koffice/kivio file) into windows readable type of file? rahmmandel Linux - Desktop 7 01-30-2007 05:12 PM
determine encoding type of a file (ie - UTF-8) chovy Linux - Software 1 04-03-2006 01:46 AM
How do I determine file system type? lowpro2k3 Linux - General 5 07-09-2005 04:40 PM
How does KDE determine file type? vdemuth Linux - Software 4 01-08-2005 05:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration