...
Wiki Markup |
---|
The TR 24731-1 {{tmpnam_s()}} function generates a string that is a valid filename and that is not the same as the name of an existing file \[[ISO/IEC TR 24731-1-2007|AA. C References#SO/IEC TR 24731-1-2007]\]. The functionIt is potentiallyalmost capableidentical ofto generatingthe {{tmpnam{TMP_MAX_S}}} differentfunction strings,above butexcept anywith oran all of them may already be in use by existing files and thus not be suitable return values. The lengths of these strings must be less than the value of the {{L_tmpnam_s}} macro. |
Non-normative text in TR 24731-1 also recommends the following:
added {{maxsize}} argument for the supplied buffer. |
Non-normative text in TR 24731-1 also recommends the following:
Implementations should take care in choosing the patterns used for names returned by tmpnam_s. Implementations should take care in choosing the patterns used for names returned by tmpnam_s. For example, making a thread id part of the names avoids the race condition and possible conflict when multiple programs run simultaneously by the same user generate the same temporary file names.
...
TR 24731-1 does not establish any criteria for predictability of names.
TR 24731-1 also notes that
After a program obtains a file name using the
tmpnam_s
function and before the
program creates a file with that name, the possibility exists that someone else may create
a file with that same name. To avoid this race condition, thetmpfile_s
function
should be used instead oftmpnam_s
when possible. One situation that requires the use
of thetmpnam_s
function is when the program needs to create a temporary directory
rather than a temporary file.
Code Block | ||
---|---|---|
| ||
/* ... */ FILE *file_ptr; char filename[L_tmpnam_s]; if (tmpnam_s(filename, L_tmpnam_s) != 0) { /* Handle Error */ } if (!fopen_s(&file_ptr, filename, "wb+")) { /* Handle Error */ } /* ... */ |
...