Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #138 Create temp file

Create a new temporary file on the filesystem.

import std.stdio;
auto f = File.tmpfile();
#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();
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"))?;

New implementation...
< >
programming-idioms.org