Mac platform Get app Path

Source: Internet
Author: User

Originally just for the Mac platform to simply call fopen, fwrite and other functions, found that fopen need to include an absolute path to normal use, otherwise, even if you can create a file during debugging, directly call the app execution, the file will not be created.

To do this, you need to get an absolute path to the execution file. Use the function _nsgetexecutablepath. The path that this function obtains is the path of the executable file in the app package, which is added directly to the following. /.. /.. /.. /' to the app path is also not possible, probably the problem of access rights, will cause fopen failure.

So first the obtained path processing, get the app path, and then call in fopen, confirmed OK.

The code is as follows:

Get app path. #include <mach-o/dyld.h>char g_path[maxpathlen+1];-(void) getapppath{    uint32_t size = sizeof ( G_path);    if (_nsgetexecutablepath (G_path, &size) = = 0)        printf ("Executable path is%s\n", g_path);    else        printf ("Buffer too small; Need size%u\n ", size);        /users/blade/proj//output/macos/bin/testtool.app/contents/macos/testtool        if (strlen (G_path))    {        int end,count = 0;        for (int i = strlen (G_path)-1; i>0; i--)        {            if (g_path[i] = = '/')            {                count++;            }            if (count = = 4)            {                end = i;                break;            }        }        if (End > 0)        {            memset (G_path+end,0,strlen (G_path)-end);        }    }}


Mac platform Get app Path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.