Thread: who to get file type using c

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    6

    who to get file type using c

    hi , every one

    can i get file type using C >>>?

    if the is passable please tell me about the way to get it ...?

    with my respect

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There are two common understandings of file type:

    1) distinguishing regular files (text, binary data, images, executables, etc) from directories, links, and other special files (such as fifos). This is mostly a *nix thing I think, where you also have files that are sockets, files that are device interfaces, etc. The file system maintains an attribute indicating whether a file is a reg file, a directory, a link, etc.

    2) file type based on extension, eg, .txt, .exe, .jpeg, etc.

    You can do both; the first one is OS specific, the second one just requires parsing the filename.
    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

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    And file types can be obfuscated somewhat, calling a .zip file .txt might make it look to the casual observer as a text file but it really isn't. Do you mean to truly distinguish such things? If so, the level of complexity just went up quite a bit (an understatement perhaps).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    6
    Thanks "MK27"

    for UR qukly answer

    yes i mean how to get type file (regular files (text, binary data, images, executables, etc))

    by using turbo c ....?

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    11
    Using stat function we can get the file type. As well as in unix we can use the stat command also.But stat command lot of situations give regular file as the type. Better we can use stat function.
    The field st_mode used to check the file type in stat function.

    Code:
    syntax:
    int stat(const char *path, struct stat *buf);
    The structure fill up with an attributes of the given file. For more details read "man 2 stat".

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    26

    Thumbs up

    Using file command also we can get the file type.
    Example
    Code:
    #include<stdio.h>
    int main(int argc,char *argv[])
    {
            system("file filetype.c");
            system("file ."); //'.' (dot) represents current directory.
    }
    Output of the above program is,
    filetype.c: ASCII C program text
    .: directory

  7. #7
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by kiruthika View Post
    Using file command also we can get the file type.
    Example
    Code:
    #include<stdio.h>
    int main(int argc,char *argv[])
    {
            system("file filetype.c");
            system("file ."); //'.' (dot) represents current directory.
    }
    Output of the above program is,
    filetype.c: ASCII C program text
    .: directory
    That doesn't return any information to the program though, that just calls that other program and returns an int specifying whether it ran correctly or not.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you want to know any useful file type info such as whether it's an AVI, MPEG, MP3... you'll need to know the file format of all those types and read the file to see if it matches the pattern. It gets pretty hard since there are so many file types (and things like AVI have many different formats) that you'd have to look around forever searching for info about file types. One of my previous companies tried to do that, and it wasn't easy.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  5. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM