Thread: detectine OS type?

  1. #1
    Registered User
    Join Date
    Dec 2009

    detectine OS type?

    Hi all,

    For my little program to run, it needs to open a file, read the contents then display it. easy yes...

    However I need to hardcode this path of the file in.

    But the location of the file is different from OS to OS, I want it to work in Linux primarily, however I need to find a way of detecting for Mac OS X??

    Is there a little function I can call that will determine the OS type?
    or is there a way I can make a function?

    Cheers all


  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Either do it at compile time, or run time.

    The first is easiest. All you have to do is check for macros that your compiler should define and specify your paths. i.e.
    #ifdef linux
       const char * prefix = "/example/path/";
       const char * prefix = "/some path for mac";
    Of course, gcc on OS X should define something like MAC -- but check the manual.

    Or if you wanted to do it at runtime, use getenv().

  3. #3
    Registered User
    Join Date
    Jan 2010
    Quote Originally Posted by zacs7 View Post
    of course, gcc on os x should define something like mac -- but check the manual.
    (should be in all caps. Dunno why but the forum changes it to small letters for me o_O)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2009

    Thumbs up

    cheers yep,

    got it working great


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. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

Tags for this Thread