Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

giha

macrumors newbie
Original poster
Nov 16, 2009
4
0
Hi,

i am trying to create a cache folder in C for the current user.
In terminal i type: mkdir ~/Library/Caches/atest and it works,
but when i write it in C it failed to work. What's wrong? Thanks!

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>


int main (int argc, const char * argv[]) {
    // insert code here...
    printf("Hello, World!\n");

	mkdir("~/Library/Caches/atest", 0777);

    return 0;
}
 

chown33

Moderator
Staff member
Aug 9, 2009
10,761
8,454
A sea of green
mkdir("~/Library/Caches/atest", 0777);
C functions don't understand the ~ convention. That's a shell feature, and the shell expands ~'s before passing a value as a command arg.

I strongly recommend adding actual error-checking to your code. If you test the return value and then examine the value of errno on errors, the reason would be a little clearer. Also see 'man perror' for a function that prints messages for errno.

Also write yourself a command that does nothing but print its args, one per line, numbered. Then run it like this:

Code:
./mycmd ~/Library/Caches/atest
and see what it outputs. In my view, this should be the second program a student writes, right after HelloWorld. It's not only a useful exercise for learning C, it's a useful tool for understanding args passed to commands.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.