Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C++
Reference
Libraries Reference
Run-Time Library
 tmpfile

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
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 );

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

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.

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.

// 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

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Win7 and C:\      Ruud van Gaal   |   Edit   |   Show History
This function fails to work on my Win7 RC currently. The tmpfile() function attempts to create a file in the root (C:\), which is has no permission for. This was, admittedly, after a machine crash. I'm trying to open up my directory, but I'm not sure why the root is suddenly closed down. Also, I find the root directory of a drive a funny location for a temporary file, but that may be just me.

(one day later)

I managed to fix it by creating my own tmpfile() function to replace the faulty default one (running Win7/build7100, Visual C++ 2008SP1):
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(1024,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;
}

Not sure if tmpfile() would create auto-deleting files, but this function just creates (but never deletes) the temp file.

Tags What's this?: root (x) win7 (x) Add a tag
Flag as ContentBug
Above is a result of everyone having kept using it with Administrators.      GreenCat ... Ruud van Gaal   |   Edit   |   Show History
http://support.microsoft.com/kb/418888
(link seems not to work)
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker