C programming tmpfile stdio.h


In C programming the <stdio.h> tmpfile creates a binary temporary file different from any other existing file.The declaration of the function is given below.

FILE *tmpfile(void);

Parameters:
void

Return type
FILE* -A pointer of FILE* type pointing to the temporary file created.If the temporary file cannot be created NULL is returned.

Some points to note:

i)The temporary file created is opened with “wb+” mode.

ii)The number of temporary files that can be opened simultaneously is given by ‘TMP_MAX‘ which must be at least 25.

iii)The temporary file is removed when the program terminates.

iv)If the temporary file is opened,when the program terminates whether the file is removed is implementation-defined.

Code example

FILE *fp=tmpfile( );

fclose(fp);

A more exhaustive code example is given below.

Code example

FILE *fp=tmpfile( ) ;

int i=1 ;

if( ffile!=NULL )
{
fputs( “New string ” , fp ); //writting string to temporary file pointed by ‘fp’
}
else
  printf(“Temporary file cannot be created”);

rewind( fp );

/*Accessing the content of the temporary file */
while( ( i=fgetc(fp)) !=EOF )
{
putchar(i);
}

fclose(fp); //closes the temporary file

Output

New string