开发者

Create a directory

I am trying to create a directory using the following code. It compiles, but it does not create a dir开发者_如何学Goectory. Any suggestions?

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
int main(void)
{
  const char base[] = "filename";
  char filename [ FILENAME_MAX ];
  int number = 42;
  sprintf(filename, "%s/%d", base, number);
  printf("filename = \"%s\"\n", filename);
  mkdir (filename, S_IRWXU);
  return 0;
}


Does the "filename" directory already exist? mkdir() will only create one directory at a time; if the parent directory doesn't exist either, you'll have to create it separately, first.


Most probably it fails to create directory because you are trying to create a nested directory and its parent does not exist. mkdir cannot create directories recursively. But you can only guess unless you properly check return codes and errors in your program.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜