jumpstart17253
jumpstart17253

Reputation: 21

How to get path to current user temporary folder on MAC Xcode C++

I am developing a Console application (Command line tool) on Xcode using C++.

How to get path to current users temporary folder.

Basically I want to create a text file on MAC OS 10.8 in users temporary folder.

Upvotes: 2

Views: 3465

Answers (2)

H. Katsura
H. Katsura

Reputation: 51

use confstr() to get the temporary directory (un-accessed files get deleted after 3 days by the system) and the cache directory (files won't get deleted by the system).

% cat confstr.c
#include <unistd.h>
#include <stdio.h>
#include <sys/syslimits.h>

int main() {
    char buf[PATH_MAX];
    size_t len = PATH_MAX;
    size_t s = confstr(_CS_DARWIN_USER_TEMP_DIR, buf, len);
    printf("_CS_DARWIN_USER_TEMP_DIR(len:%d): %s\n", (int)s, buf);
    s = confstr(_CS_DARWIN_USER_CACHE_DIR, buf, len);
    printf("_CS_DARWIN_USER_CACHE_DIR(len:%d): %s\n", (int)s, buf);
    return 0;
}
% cc confstr.c -o confstr -mmacosx-version-min=10.11 -arch x86_64 -arch arm64
% ./confstr     
_CS_DARWIN_USER_TEMP_DIR(len:50): /var/folders/hg/rprr5xfn1vsd17dbtzqttg640000gn/T/
_CS_DARWIN_USER_CACHE_DIR(len:50): /var/folders/hg/rprr5xfn1vsd17dbtzqttg640000gn/C/
% sudo ./confstr         
_CS_DARWIN_USER_TEMP_DIR(len:50): /var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T/
_CS_DARWIN_USER_CACHE_DIR(len:50): /var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/C/

Upvotes: 1

smaryus
smaryus

Reputation: 349

See getenv("TMPDIR")

If it returns the needed path.

This is a list with all the env variables.

https://developer.apple.com/library/mac/#documentation/Darwin/Reference/Manpages/man7/environ.7.html#//apple_ref/doc/man/7/environ

You can use mktemp to create a file in the temp directory https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/mktemp.3.html


Upvotes: 1

Related Questions