tempnam函数和tmpfile函数

在soap的示例程序中(dime)用到了其中的tempnam函数创建一个以随机字符串结尾而命名的临时文件来保存接收到的数据。

################################################################################################################

NAME

mkstemp - make a unique filename

SYNOPSIS

[XSI] [Option Start] #include <stdlib.h>

int mkstemp(char *template);

DESCRIPTION

The mkstemp() function shall replace the contents of the string pointed to bytemplate by a unique filename, andreturn a file descriptor for the file open for reading and writing. The function thus prevents any possible race condition betweentesting whether the file exists and opening it for use. The string intemplate should look like a filename with six trailing'X' s;mkstemp() replaces each 'X' with a character from the portable filename character set. Thecharacters are chosen such that the resulting name does not duplicate the name of an existing file at the time of a call tomkstemp().

RETURN VALUE

Upon successful completion, mkstemp() shall return an open file descriptor. Otherwise, -1 shall be returned if nosuitable file could be created.

ERRORS

No errors are defined.

EXAMPLES

Generating a Filename

The following example creates a file with a 10-character name beginning with the characters"file" and opens the filefor reading and writing. The value returned as the value offd is a file descriptor that identifies the file.

#include <stdlib.h>
...
char template[] = "/tmp/fileXXXXXX";
int fd;


fd = mkstemp(template);

APPLICATION USAGE

It is possible to run out of letters.

The mkstemp() function need not check to determine whether the filename part oftemplate exceeds the maximumallowable filename length.

#########################################################################################################################

NAME

tempnam - create a name for a temporary file

SYNOPSIS

[XSI] [Option Start] #include <stdio.h>

char *tempnam(const char *dir, const char *pfx);

DESCRIPTION

The tempnam() function shall generate a pathname that may be used for a temporary file.

The tempnam() function allows the user to control the choice of a directory. Thedir argument points to the nameof the directory in which the file is to be created. Ifdir is a null pointer or points to a string which is not a name foran appropriate directory, the path prefix defined as P_tmpdir in the<stdio.h>header shall be used. If that directory is not accessible, an implementation-defined directory may be used.

Many applications prefer their temporary files to have certain initial letter sequences in their names. Thepfx argumentshould be used for this. This argument may be a null pointer or point to a string of up to five bytes to be used as the beginningof the filename.

Some implementations of tempnam() may use tmpnam() internally. On suchimplementations, if called more than {TMP_MAX} times in a single process, the behavior is implementation-defined.

RETURN VALUE

Upon successful completion, tempnam() shall allocate space for a string, put the generated pathname in that space, andreturn a pointer to it. The pointer shall be suitable for use in a subsequent call tofree(). Otherwise, it shall return a null pointer and seterrno to indicate theerror.

ERRORS

The tempnam() function shall fail if:

[ENOMEM]
Insufficient storage space is available.

EXAMPLES

Generating a Pathname

The following example generates a pathname for a temporary file in directory /tmp, with the prefix file. After thefilename has been created, the call tofree() deallocates the space used to store thefilename.

#include <stdio.h>
#include <stdlib.h>
...
char *directory = "/tmp";
char *fileprefix = "file";
char *file;


file = tempnam(directory, fileprefix);
free(file);

APPLICATION USAGE

This function only creates pathnames. It is the application's responsibility to create and remove the files. Between the time apathname is created and the file is opened, it is possible for some other process to create a file with the same name. Applicationsmay find tmpfile() more useful.

你可能感兴趣的:(String,function,File,application,character,Descriptor)