mkdir in C does not work

Discussion in 'Mac Programming' started by giha, Nov 20, 2009.

  1. giha macrumors newbie

    Nov 16, 2009

    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!

    #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;
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    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:

    ./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.
  3. giha thread starter macrumors newbie

    Nov 16, 2009

Share This Page