Logo

Programming-Idioms

Create a new temporary file on the filesystem.
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
#include <stdlib.h>
const char tmpl[] = "XXXXXX.tmp";
int fd = mkstemp(tmpl);
(ns programming-idioms
  (:import [java.io File]))
(File/createTempFile "prefix" "suffix" (new File "/tmp"))
(ns programming-idioms
  (:import [java.io File]))
(File/createTempFile "prefix" "suffix")
using System.IO;
string file = Path.GetTempFileName();
import std.stdio;
auto f = File.tmpfile();
open (newunit=u,status="scratch")
import "os"
tmpfile, err := os.CreateTemp("", "")
import System.IO.Temp
withTempFile $ \f -> do
  -- do something with f
{
  "dependencies": "tempy^1.0.1"
}

import tempy from 'tempy'
const resultOfCallback = tempy.file.task(tempFile => {
 // do something with tempFile
})
{
  "dependencies": "tempy^1.0.1"
}

import tempy from 'tempy'
const tempFile = tempy.file()
import java.io.File;
File tempFile = File.createTempFile("prefix-", "-suffix");
tempFile.deleteOnExit();
file = io.tmpfile()
$f = tmpfile();
uses sysutils;
var
  H: THandle;
begin
  H := FileCreate(GetTempFileName);
end.
use Path::Tiny qw(tempfile);
my $path = tempfile;
import File::Temp qw(tempfile);
my $fh = tempfile();
# or
my ($fh, $filename) = tempfile();


import tempfile
file = tempfile.TemporaryFile()
require 'tempfile'
file = Tempfile.new('foo') 
use tempdir::TempDir;
use std::fs::File;
let temp_dir = TempDir::new("prefix")?;
let temp_file = File::open(temp_dir.path().join("file_name"))?;