detectine OS type?

This is a discussion on detectine OS type? within the C Programming forums, part of the General Programming Boards category; Hi all, For my little program to run, it needs to open a file, read the contents then display it. ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    17

    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

    Techevo

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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.
    Code:
    #ifdef linux
       const char * prefix = "/example/path/";
    #else
       const char * prefix = "/some path for mac";
    #endif
    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
    Posts
    412
    Quote Originally Posted by zacs7 View Post
    of course, gcc on os x should define something like mac -- but check the manual.
    __apple__
    (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
    Location
    The edge of the known universe
    Posts
    32,830
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    17

    Thumbs up

    cheers yep,

    got it working great


    techevo

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, 03: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-17-2006, 12:35 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 09:57 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21