---
title: "UNIX创建临时文件"
author: "vergilwang"
date: "2011-11-04T13:26:00Z"
tags: []
categories: ["编程语言"]
slug: "unix"
draft: false
lastmod: "2014-02-15T16:26:23Z"
id: 2011430
url: "https://vergilwang.iteye.com/blog/2011430"
---

/*  
\* =====================================================================================  
\*  
\* Filename: tmp\_file.c  
\*  
\* Description:  
\*  
\* Version: 1.0  
\* Created: 2011年11月04日 11时54分36秒  
\* Revision: none  
\* Compiler: gcc  
\*  
\* Author: Wang Ran (), wangran51@126.com  
\* Company:  
\*  
\* =====================================================================================  
\*/

#include &lt;fcntl.h&gt;  
#include &lt;unistd.h&gt;  
#include &lt;stdio.h&gt;  
#include &lt;stdlib.h&gt;

int main()  
{  
int tmp = open("tmp.txt", O\_CREAT|O\_EXCL, S\_IRUSR|S\_IWUSR|S\_IXUSR);//O\_CREAT按照mode中给出的模式创建文件，O\_EXCL如果文件已经存在则open失败  
unlink("tmp.txt");  
return EXIT\_SUCCESS;

}

/*

当一个文件的链接数减少到0，且没有进程打开它，这文件就会被删除，目录立刻被删除，而文件占用的空间等到最后一个进程关闭它之后被系统回收。

 

\*/