function
<cstdio>

tmpfile

FILE * tmpfile ( void );
Open a temporary file
Creates a temporary binary file, open for update ("wb+" mode, see fopen for details) with a filename guaranteed to be different from any other existing file.

The temporary file created is automatically deleted when the stream is closed (fclose) or when the program terminates normally. If the program terminates abnormally, whether the file is deleted depends on the specific system and library implementation.

Parameters

none

Return Value

If successful, the function returns a stream pointer to the temporary file created.
On failure, NULL is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* tmpfile example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char buffer [256];
  FILE * pFile;
  pFile = tmpfile ();

  do {
    if (!fgets(buffer,256,stdin)) break;
    fputs (buffer,pFile);
  } while (strlen(buffer)>1);

  rewind(pFile);

  while (!feof(pFile)) {
    if (fgets (buffer,256,pFile) == NULL) break;
    fputs (buffer,stdout);
  }

  fclose (pFile);
  return 0;
}

This program creates a temporary file to store the lines entered by the user. When the user enters an empty line, the program rewinds the temporary file and prints its contents to stdout.

See also