Skip to Content
C in a Nutshell
book

C in a Nutshell

by Peter Prinz, Tony Crawford
December 2005
Beginner to intermediate
618 pages
20h 19m
English
O'Reilly Media, Inc.
Content preview from C in a Nutshell

Name

tmpnam

Synopsis

Generates a unique filename

#include <stdio.h>
char *tmpnam( char *s );

The tmpnam() function generates a unique filename suitable for using for temporary files, and returns a pointer to the name string. If the pointer argument s is not a null pointer, then tmpnam() places the name string in a buffer addressed by s. The size of the buffer is assumed to be at least equal to the macro L_tmpnam. If s is a null pointer, then the return value points to the filename in tmpnam()’s internal, static buffer, where it may be modified by subsequent tmpnam() calls.

If you use a name supplied by tmpnam() to create a file, that does not mean the file is a temporary file in the sense of tmpfile(); it will not be automatically deleted on closing.

The tmpnam() function generates a different name each time it is called, and can generate at least TMP_MAX distinct names (some of which may be used by tmpfile()). The macros L_tmpnam and TMP_MAX are defined in stdio.h. TMP_MAX is greater than or equal to 25. The tmpnam() function returns a null pointer on failure.

Example

char buffer[L_tmpnam], *name = buffer;
FILE *fpOut;
int result;

name =tmpnam( buffer );

if ( name == NULL )
{
  fputs( "Failed to generate temporary file name", stderr );
  return -1;
}

fpOut = fopen( name, "w+" );
/* ... write something in the file, edit it ... */
fclose( fpOut );
printf( "Results saved in %s\n", name );
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C in a Nutshell, 2nd Edition

C in a Nutshell, 2nd Edition

Peter Prinz, Tony Crawford
Smaller C

Smaller C

Marc Loy
C Pocket Reference

C Pocket Reference

Peter Prinz, Ulla Kirch-Prinz
Extreme C

Extreme C

Kamran Amini

Publisher Resources

ISBN: 0596006977Errata Page