Alphabetical Function Refer ...


Run-Time Library Reference 
tmpfile 

Creates a temporary file. This function is deprecated because a more secure version is available; see tmpfile_s.

FILE *tmpfile( void );
Return Value

If successful, tmpfile returns a stream pointer. Otherwise, it returns a NULL pointer.

Remarks

The tmpfile function creates a temporary file and returns a pointer to that stream. The temporary file is created in the root directory. To create a temporary file in a directory other than the root, use tmpnam or tempnam in conjunction with fopen.

If the file cannot be opened, tmpfile returns a NULL pointer. This temporary file is automatically deleted when the file is closed, when the program terminates normally, or when _rmtmp is called, assuming that the current working directory does not change. The temporary file is opened in w+b (binary read/write) mode.

Failure can occur if you attempt more than TMP_MAX (see STDIO.H) calls with tmpfile.

Requirements

Routine Required header Compatibility

tmpfile

<stdio.h>

ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

For additional compatibility information, see Compatibility in the Introduction.

Example

// crt_tmpfile.c
// compile with: /W3
// This program uses tmpfile to create a
// temporary file, then deletes this file with _rmtmp.
#include <stdio.h>

int main( void )
{
   FILE *stream;
   char tempstring[] = "String to be written";
   int  i;

   // Create temporary files.
   for( i = 1; i <= 3; i++ )
   {
      if( (stream = tmpfile()) == NULL ) // C4996
      // Note: tmpfile is deprecated; consider using tmpfile_s instead
         perror( "Could not open new temporary file\n" );
      else
         printf( "Temporary file %d was created\n", i );
   }

   // Remove temporary files.
   printf( "%d temporary files deleted\n", _rmtmp() );
}

Output

Temporary file 1 was created
Temporary file 2 was created
Temporary file 3 was created
3 temporary files deleted
.NET Framework Equivalent

Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.

See Also

Reference

Stream I/O
_rmtmp
_tempnam, _wtempnam, tmpnam, _wtmpnam

Tags :


Community Content

Ruud van Gaal
Win7 and C:\
This function fails to work on Vista/Win7 RC currently. The tmpfile() function on Win32 attempts to create a file in the root (C:\), which is has no permission for. You'd expect temporary files to be created in a temporary location, but this has not been implemented yet (VS2008SP1 currently) so on Windows this function has become a non-no. I did the following code, but it generates a lot of non-removed temp files.
A better attempt is available at http://lists.cairographics.org/archives/cairo/2007-December/012561.html

A simple fix for MS would be to create a temp file in a temporary directory, as it should be. It's already weird that temp directories don't get cleaned upon every reboot (an explicit removal seems to be needed out of the box, which is just bad design, fixed with a minimum of effort). Not trying to bash Windows, just not understand why you wouldn't copy useful simple functionality from other OSes.

My initial code:
FILE *myTmpFile()
// Creates a temp file in a more readable location than tmpfile() on Win32
// (which writes in root, not allowed in Vista/Win7)
{
char buf[1024],buf2[1024],bufN[256];
int n;
FILE *fp;

GetTempPath(sizeof(buf),buf);
for(n=_getpid();;n++)
{
strcpy_s(buf2,sizeof(buf2),buf);
sprintf_s(bufN,sizeof(bufN),"jpeg_rvg_%d",n);
strcat_s(buf2,sizeof(buf2),bufN);
fp=0;
// Exists?
fp=fopen(buf2,"rb");
if(fp)
{
fclose(fp);
continue;
}
// Clear file and allow reads
fopen_s(&fp,buf2,"wb+");
if(fp)return fp;
}
// We'll never get here
return 0;
}

Tags : vista root win7

Ruud van Gaal
Above is a result of everyone having kept using it with Administrators.
http://support.microsoft.com/kb/418888
(link seems not to work)
Tags :

Ruud van Gaal
Any user should be able to create a temporary file
(reply to the 'Administrators' reply above)

Not sure what creating a temporary file has to do with being administrator or not. Every user should be able to create a temporary file, and it's really trivial. However, on Windows you really want to abstract most operations so you can move along when OS'es SDK's change and bring incompatibilities in trivial stuff.
Stating this has to do with being Adminstrators is a clear sign of not understanding the problem, namely that a perfectely functional name (tmpfile) should just do what it says, and is trivial to maintain throughout OSes and SDK versions, as it's very trivial. It just doesn't get the attention it needs.
Tags :

Page view tracker