w3resource

C tmpnam() function

C library function - tmpnam()

The tmpnam() function is used to generate a string that is a valid pathname that does not name an existing file.

Syntax:

char *tmpnam(char *s);

tmpnam() Parameters:

Name Description Required /Optional
s Stores the file name in s. Required

Return value from tmpnam()

  • Upon successful completion, tmpnam() shall return a pointer to a string.
  • If no suitable string can be generated, the tmpnam() function shall return a null pointer.

Example: tmpnam() function.

Following example calls tmpnam() function to create a valid file name.

#include <stdio.h>
 
int main(void)
{
   char *file_name;
   if ((file_name = tmpnam(NULL)) !=NULL)
      printf("%s can be used as a file name.\n", file_name);
   else 
      printf("Cannot create a unique file name\n");
 
}

Output:

\sge4. can be used as a file name.

C Programming Code Editor:

Previous C Programming: C tmpfile()
Next C Programming: C fprintf()



Follow us on Facebook and Twitter for latest update.