Secure temporary file: Difference between revisions

Content added Content deleted
(Scala)
Line 22: Line 22:


=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<lang c>#include <stdlib.h>
#include <stdio.h>


int main() {
int main(void)
{
FILE *fh = tmpfile(); /* file is automatically deleted when program exits */
FILE *fh = tmpfile(); /* file is automatically deleted when program exits */
/* do stuff with stream "fh" */
/* do stuff with stream "fh" */
fclose(fh);
(void) fclose(fh);
return EXIT_SUCCESS;
/* The C standard library also has a tmpnam() function to create a file
for you to open later. But you should not use it because someone else might
be able to open the file from the time it is created by this function to the
time you open it. */
return 0;
}</lang>
}</lang>


Line 41: Line 37:
#include <stdio.h>
#include <stdio.h>


int main() {
int main(void)
{
char filename[] = "/tmp/prefixXXXXXX";
const char *filename = "/tmp/prefixXXXXXX";
int fd = mkstemp(filename);
puts(filename);
const int fd = mkstemp(filename);
(void) puts(filename);
/* do stuff with file descriptor "fd" */
/* do stuff with file descriptor "fd" */
close(fd);
(void) close(fd);
return 0;
return EXIT_SUCCESS;
}</lang>
}</lang>