Create temporary files in Linux

Source: Internet
Author: User
Moved to: http://www.wypblog.com/archives/172

A temporary file is a file temporarily used to store data. If you use the method of creating a common file to create a file, you may encounter the problem of whether the file exists and whether the file has the read and write permissions. The following describes how to create a unique temporary file in Linux:

 
# Include <stdio. h> char * tmpnam (char * s); file * tmpfile ();

The tmpnam () function generates a unique I file name. If the parameter is null, a file name is generated in an internal buffer. When this function is called one time, the file name may be overwritten. If the parameter is not null, the name is copied to the string. The length of the string must be at least l_tmpnam, which is defined in stdio. h. If it fails, the function returns NULL. The tmpfile () function returns the descriptor of a temporary file. The open attribute of the file is read and write. It is equivalent to using fopen to open with W +. If it fails, null is returned.

The following is a short column.

# Include <stdio. h> int main () {char tmpname [l_tmpnam]; char * filename; file * FP; filename = tmpname (tmpname ); printf ("temporary file name is % s \ n", filename); FP = tmpfile (); If (FP) {printf ("temporary file opened! \ N ");} else {perror (" tmpfile ");} return 0 ;}

In addition, there are two functions that can also create temporary files:

 
# Include <stdio. h> char * mktemp (char * template); int mkstemp (char * template );

The mktemp () function creates a unique temporary file name from the given module template. The template can be the prefix of the file path. The last six characters of the template must be "xxxxxx". For example:

 
Char template [l_tmpnam]; char * filename; strcpy (template, "/temp/wypxxxxxx"); filename = mktemp (Template ); printf ("temporary file name is % s \ n", filename );

The mkstemp () function is similar to tmpfile (), but the opened file is equivalent to opening the file using the underlying file operation function open.

Reprinted Please note: Reprinted from past memories (http://www.wypblog.com /)

Link: creating temporary files in Linux (http://www.wypblog.com/archives/172)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.