tmpfile - C stdio.h

C examples for stdio.h:tmpfile

Type

function

From


<cstdio>
<stdio.h>

Description

Open a temporary file for update ("wb+" mode) with a unique filename.

The temporary file created is automatically deleted when the stream is closed (fclose) or when the program terminates normally.

Prototype

FILE * tmpfile ( void );

Parameters

none

Return Value

On success, the function returns a stream pointer to the temporary file.

On failure, NULL is returned.

Demo Code

#include <stdio.h>
#include <string.h>

int main ()/*from  w ww .j  a v  a 2 s .  co  m*/
{
  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;
}

Related Tutorials