简体   繁体   English

如何在C中创建临时目录?

[英]How to create a temporary directory in C?

I am trying to create a temporary directory to perform some operations in it and then delete the whole thing at the end. 我正在尝试创建一个临时目录来执行其中的一些操作,然后在最后删除整个事情。 I use C language in a UNIX system, so I would like to have some compliance with this environment. 我在UNIX系统中使用C语言,所以我希望能够遵守这个环境。

What is the best way to program this ? 对此进行编程的最佳方法是什么?

EDIT I really need a directory, not only a file. 编辑我真的需要一个目录,而不仅仅是一个文件。 The small program is intended to try out if I can perform an svn checkout of a project. 小程序旨在尝试我是否可以执行项目的svn checkout So, it should be able to create a full hierarchy of files and directories. 因此,它应该能够创建文件和目录的完整层次结构。

You should use mkdtemp function. 你应该使用mkdtemp函数。

#define  _POSIX_C_SOURCE 200809L

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
        char template[] = "/tmp/tmpdir.XXXXXX";
        char *dir_name = mkdtemp(template);

        if(dir_name == NULL)
        {
                perror("mkdtemp failed: ");
                return 0;
        }

        /* Use it here */
        printf("%s", dir_name);



        /* Don't forget to delete the folder afterwards. */
        if(rmdir(dir_name) == -1)
        {
                perror("rmdir failed: ");
                return 0;
        }


        return 0;

}

Don't forget to delete the directory afterwards! 不要忘记之后删除目录!

I suggest to use the mkdtemp() function together with usual functions from the C API ( glibc ). 我建议将mkdtemp()函数与C API( glibc )中的常用函数一起使用。 Here is a full answer: 这是一个完整的答案:

EDIT : The answer from Nemanja Boric is unfortunately not usable in practice because the rmdir() function is only intended to remove an empty directory. 编辑 :遗憾的是,Nemanja Boric答案在实践中无法使用,因为rmdir()函数仅用于删除空目录。 Here is a full correct answer: 这是一个完整正确的答案:

#define  _POSIX_C_SOURCE 200809L
#define  _XOPEN_SOURCE 500L

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <errno.h>
#include <ftw.h>

/* Call-back to the 'remove()' function called by nftw() */
static int
remove_callback(const char *pathname,
                __attribute__((unused)) const struct stat *sbuf,
                __attribute__((unused)) int type,
                __attribute__((unused)) struct FTW *ftwb)
{
  return remove (pathname);
}

int
main ()
{
  /* Create the temporary directory */
  char template[] = "/tmp/tmpdir.XXXXXX";
  char *tmp_dirname = mkdtemp (template);

  if (tmp_dirname == NULL)
  {
     perror ("tempdir: error: Could not create tmp directory");
     exit (EXIT_FAILURE);
  }

  /* Change directory */
  if (chdir (tmp_dirname) == -1)
  {
     perror ("tempdir: error: ");
     exit (EXIT_FAILURE);
  }

  /******************************/
  /***** Do your stuff here *****/
  /******************************/

  /* Delete the temporary directory */
  if (nftw (tmp_dirname, remove_callback, FOPEN_MAX,
            FTW_DEPTH | FTW_MOUNT | FTW_PHYS) == -1)
    {
      perror("tempdir: error: ");
      exit(EXIT_FAILURE);
    }

  return EXIT_SUCCESS;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM